构造器注入
- 前面已经讲过,在构造器中传入相关属性
public class SimpleMovieLister {
// the SimpleMovieLister has a dependency on a MovieFinder
private MovieFinder movieFinder;
// a constructor so that the Spring container can inject a MovieFinder
public SimpleMovieLister(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
// business logic that actually uses the injected MovieFinder is omitted...
}
Set注入(重点)
- 依赖:Bean对象的创建依赖于容器
- 注入:Bean对象的所有属性,由容器来注入
距离说明
- 复杂类型
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Address{" +
"address='" + address + '\'' +
'}';
}
}
- 真实对象
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbys;
private Map<String,String> card;
private Set<String> games;
private String wife;
private Properties info;
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address +
", books=" + Arrays.toString(books) +
", hobbys=" + hobbys +
", card=" + card +
", games=" + games +
", wife='" + wife + '\'' +
", info=" + info +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String[] getBooks() {
return books;
}
public void setBooks(String[] books) {
this.books = books;
}
public List<String> getHobbys() {
return hobbys;
}
public void setHobbys(List<String> hobbys) {
this.hobbys = hobbys;
}
public Map<String, String> getCard() {
return card;
}
public void setCard(Map<String, String> card) {
this.card = card;
}
public Set<String> getGames() {
return games;
}
public void setGames(Set<String> games) {
this.games = games;
}
public String getWife() {
return wife;
}
public void setWife(String wife) {
this.wife = wife;
}
public Properties getInfo() {
return info;
}
public void setInfo(Properties info) {
this.info = info;
}
}
- 常量注入
<bean id="student" class="com.byx.pojo.Student">
<property name="name" value="qin"/>
</bean>
- Bean注入
<bean id="address" class="com.byx.pojo.Address">
<property name="address" value="xian"/>
</bean>
<bean id="student" class="com.byx.pojo.Student">
<!--第一种,普通值注入,value-->
<property name="name" value="qin"/>
<!--第二种,Bean注入,ref-->
<property name="address" ref="address"/>
</bean>
- 其余注入,完善
<bean id="address" class="com.byx.pojo.Address">
<property name="address" value="xian"/>
</bean>
<bean id="student" class="com.byx.pojo.Student">
<!--第一种,普通值注入,value-->
<property name="name" value="qin"/>
<!--第二种,Bean注入,ref-->
<property name="address" ref="address"/>
<!--数组注入-->
<property name="books">
<array>
<value>红楼梦</value>
<value>水浒传</value>
<value>西游记</value>
<value>三国演义</value>
</array>
</property>
<!--List注入-->
<property name="hobbys">
<list>
<value>tingge</value>
<value>dianying</value>
<value>code</value>
</list>
</property>
<!--Map注入-->
<property name="card">
<map>
<entry key="shenfenzheng" value="123456"/>
<entry key="yinhangka" value="123456123456789"/>
</map>
</property>
<!--Set注入-->
<property name="games">
<set>
<value>LOL</value>
<value>COC</value>
<value>BOB</value>
</set>
</property>
<!--NULL-->
<property name="wife">
<null/>
</property>
<!--Propeties-->
<property name="info">
<props>
<prop key="学号">123</prop>
<prop key="性别">男</prop>
</props>
</property>
</bean>
拓展方式
- P命名空间注入 : 需要在头文件中加入约束文件
- 导入约束 : xmlns:p="http://www.springframework.org/schema/p
<!--P(属性: properties)命名空间 , 属性依然要设置set方法-->
<bean id="user" class="com.kuang.pojo.User" p:name="xx" p:age="18"/>
- c 命名空间注入 : 需要在头文件中加入约束文件
- 导入约束 : xmlns:c="http://www.springframework.org/schema/c
<!--C(构造: Constructor)命名空间 , 属性依然要设置set方法-->
<bean id="user" class="com.kuang.pojo.User" c:name="xx" c:age="18"/>
发现问题:报错了,没有写有参构造!
解决:把有参构造器加上,这里也能知道,c 就是所谓的构造器注入!
Bean的作用域
在Spring中,那些组成应用程序的主体及由Spring IoC容器所管理的对象,被称之为bean。简单地讲,bean就是由IoC容器初始化、装配及管理的对象。
几种作用域中,request、session作用域仅在基于web的应用中使用(不必关心你所采用的是什么web应用框架),只能用在基于web的Spring ApplicationContext环境。
Singleton
- 当一个bean的作用域为Singleton,那么Spring IoC容器中只会存在一个共享的bean实例,并且所有对bean的请求,只要id与该bean定义相匹配,则只会返回bean的同一实例。Singleton是单例类型,就是在创建起容器时就同时自动创建了一个bean的对象,不管你是否使用,他都存在了,每次获取到的对象都是同一个对象。注意,Singleton作用域是Spring中的缺省作用域。要在XML中将bean定义成singleton,可以这样配置:
- xml
<bean id="ServiceImpl" class="cn.csdn.service.ServiceImpl" scope="singleton">
- Test
@Test
public void test03(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = (User) context.getBean("user");
User user2 = (User) context.getBean("user");
System.out.println(user==user2);
}
- 结果为true
Prototype
- 当一个bean的作用域为Prototype,表示一个bean定义对应多个对象实例。Prototype作用域的bean会导致在每次对该bean请求(将其注入到另一个bean中,或者以程序的方式调用容器的getBean()方法)时都会创建一个新的bean实例。Prototype是原型类型,它在我们创建容器的时候并没有实例化,而是当我们获取bean的时候才会去创建一个对象,而且我们每次获取到的对象都不是同一个对象。根据经验,对有状态的bean应该使用prototype作用域,而对无状态的bean则应该使用singleton作用域。在XML中将bean定义成prototype,可以这样配置:
- xml
<bean id="account" class="com.foo.DefaultAccount" scope="prototype"/>
或者
<bean id="account" class="com.foo.DefaultAccount" singleton="false"/>
- 通过上述Test代码,结果为false。