spring overview
實(shí)現(xiàn)
實(shí)體類定義
@Bean
public class Blog {
private Integer id;
private String title;
private String[] comments;
private Person author;
private List<Person> follows;
private Map<String, String> changes;
private Set<Person> like;
private Properties info;
}
bean 配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- more bean definitions go here -->
<bean id="person" class="cn.spring.pojo.Person">
<property name="name" value="張三"/>
<property name="id" value="1"/>
</bean>
<bean id="blog" class="cn.spring.pojo.Blog">
<!-- 第一種注入,普通值注入恼策, 使用value屬性-->
<property name="title" value="a blog"/>
<!-- 第二種注入僵刮,bean注入塑顺,使用ref屬性-->
<!-- <property name="author" ref="person"/>-->
<property name="author">
<bean class="cn.spring.pojo.Person">
<property name="id" value="2"/>
<property name="name" value="李四"/>
</bean>
</property>
<!-- 第三種注入栗柒,數(shù)組注入亚皂,使用array value標(biāo)簽組-->
<property name="comments">
<array>
<value>this is a comment</value>
<value>this is a comment</value>
<value>this is a comment</value>
<value>this is a comment</value>
</array>
</property>
<!-- 第四中注入,列表注入廊移,使用list標(biāo)簽 其中嵌套bean標(biāo)簽或者value標(biāo)簽-->
<property name="follows">
<list>
<bean id="person1" class="cn.spring.pojo.Person">
<property name="id" value="3"/>
<property name="name" value="王五"/>
</bean>
<bean id="person2" class="cn.spring.pojo.Person">
<property name="id" value="4"/>
<property name="name" value="田六"/>
</bean>
</list>
</property>
<!-- 第五種注入扭粱,map注入舵鳞,使用map entry標(biāo)簽對(duì)-->
<property name="changes">
<map>
<entry key="第一次改動(dòng)" value="改動(dòng)內(nèi)容"/>
<entry key="第二次改動(dòng)" value="改動(dòng)內(nèi)容"/>
</map>
</property>
<!-- 第六種注入,set注入焊刹, 使用set標(biāo)簽-->
<property name="like">
<set>
<ref bean="person"/>
</set>
</property>
<!-- 第七種注入系任,空值注入,一種使用null標(biāo)簽虐块,第二種將value值設(shè)為空串就行-->
<!-- <property name="id">-->
<!-- <null/>-->
<!-- </property>-->
<property name="id" value=""/>
<!-- 第八中輸入俩滥,property注入,使用proos 和prop標(biāo)簽對(duì)-->
<property name="info">
<props>
<prop key="link">www.baidu.com</prop>
</props>
</property>
</bean>
</beans>
結(jié)果
@Test
public void iocTest() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
Blog blog = applicationContext.getBean(Blog.class);
/*
* Blog
* (id=null,
* title=a blog,
* comments=[this is a comment, this is a comment, this is a comment, this is a comment],
* author=Person(id=2, name=李四),
* follows=[Person(id=3, name=王五), Person(id=4, name=田六)],
* changes={第一次改動(dòng)=改動(dòng)內(nèi)容, 第二次改動(dòng)=改動(dòng)內(nèi)容},
* like=[Person(id=1, name=張三)],
* info={link=www.baidu.com})
* */
System.out.println(blog);
}
注意點(diǎn)
bean注入
使用bean注入可以使用兩種方法
- 一是使用自閉和標(biāo)簽用ref屬性來指定一個(gè)定義過的bean
<property name="author" ref="person"/>
- 第二種就是使用內(nèi)嵌的bean子標(biāo)簽贺奠,值得注意的是霜旧,此時(shí)定義的這個(gè)bean并不會(huì)能被其他bean所調(diào)用,類似于java中匿名內(nèi)部類(就算你在這個(gè)bean標(biāo)簽內(nèi)使用id和name屬性來命名儡率,在外部也是無法檢測(cè)到的)至于它是實(shí)現(xiàn)方法是不是就是使用java中的匿名內(nèi)部類挂据,由于沒看過spring源碼就不去猜測(cè)了。
set list array 和 map 下的entry
這幾個(gè)標(biāo)簽下都可以嵌套根據(jù)你在實(shí)體類中的定義嵌套使用 value儿普, bean崎逃, ref, idref眉孩, props个绍, null以及他們自生這幾個(gè)標(biāo)簽,總之就是對(duì)應(yīng)java類進(jìn)行出力
空值注入
控制注入也有兩種方式浪汪,但是這兩種方式所得到的結(jié)果是不一樣的巴柿!
- 一是使用
<null/>
標(biāo)簽 - 而是是value的值為空字符串,即:
<property name="id" value=""/>
他們兩個(gè)在String類型下得到結(jié)果自然是一一對(duì)應(yīng)的""和null死遭。而如果為其他java基本數(shù)據(jù)類型的話兩個(gè)都為null广恢。如果為其他類型使用空串進(jìn)行注入則會(huì)報(bào)錯(cuò)。其中byte類兩種方式都會(huì)報(bào)錯(cuò)呀潭,暫時(shí)還不清楚它具體的判斷邏輯钉迷。
spring注解開發(fā)
使用注解配置
@Configuration
使用該注解是一個(gè)類變成一個(gè)spring配置類。該注解本身也是一個(gè)Component容器蜗侈,會(huì)將類掃描到spring容器中篷牌。
- 配合的注解:
-
@ComponentScan
指定掃描的包名 -
@Bean
注冊(cè)beans -
@Import
合并多個(gè)配置類
-
掃描
``
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<context:component-scan base-package="xxx"/>
</beans>
自動(dòng)注入
@Autowried
-
@Autowired
可以在屬性上使用,也可以在set方法上使用踏幻。
可以配合required屬性或者@Nullable
注解使用使得這個(gè)對(duì)象可以為null - 配合
@Qualifier(value="bean")
使用可以指定哪一個(gè)具體的bean作為注入對(duì)象 -
@Resource(name="bean")
的效果類似于@Autowired
的效果,但是@Autowired
是byType選擇戳杀,配合@Qualifier
可以做到byName该面。而@Resource(name="bean")
默認(rèn)byName夭苗,如果找不到,還會(huì)繼續(xù)用byType進(jìn)行查找隔缀。
@Component
@Component
該注解下的類都會(huì)被掃描到Ioc容器中配合@Value
可以對(duì)bean進(jìn)行配置题造,與之有相同功能的還有@Service @Controller @Repository
他們都是@Component
的別名,有這一項(xiàng)的效果猾瘸,一般用于幫助程序員區(qū)分各個(gè)層級(jí)界赔。
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Indexed
public @interface Component {
String value() default "";
}
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Repository {
@AliasFor(
annotation = Component.class
)
String value() default "";
}
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Service {
@AliasFor(
annotation = Component.class
)
String value() default "";
}
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Controller {
@AliasFor(
annotation = Component.class
)
String value() default "";
}
@Scope
指定bean的作用域,有prototype牵触, singleton淮悼,request,session揽思,application, websocket 這六種(后四種為webAppliction專屬)袜腥。 默認(rèn)為單例模式(singleton)