概念
- 依赖注入(Dependency Injection,DI)
- 依赖 : 指Bean对象的创建依赖于容器 . Bean对象的依赖资源
- 注入 : 指Bean对象所依赖的资源 , 由容器来设置和装配
1. 构造器注入
我们在之前的案例已经讲过了2. Set方式注入【重点】
【环境搭建】
- 复杂类型属性
- 真实测试对象
要求被注入的属性,必须有set方法,set方法的方法名有set +属性首字母大写,如果属性是boolean类型,没有set方法,是is
1)pojo包
public class Address {private String address;public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}}
package cn.edu.jxust.pojo;import java.util.*;public class Student {private String name;private Address address;private String[] books;private List<String> hobbys;private Map<String,String> card;private Set<String> game;private String wife;private Properties info;@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", address=" + address +", books=" + Arrays.toString(books) +", hobbys=" + hobbys +", card=" + card +", game=" + game +", wife='" + wife + '\'' +", info=" + info +'}';}public String getName() {return name;}public void setName(String name) {this.name = name;}}
2)beans.xml文件
<?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/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd"><!-- 第一种,普通值注入,value--><bean id="student" class="cn.edu.jxust.pojo.Student"><property name="name" value="浔阳江头月送客,枫叶荻花秋瑟瑟" /></bean></beans>
3)MyTest测试
import cn.edu.jxust.pojo.Student;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class MyTest {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");Student student = (Student) context.getBean("student");System.out.println(student.getName());}}
3. 拓展方式注入
常量注入、Bean注入、数组、List、Map、Set注入、Null注入、properties注入、p命名和c命名
1)beans.xml文件
<?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/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="address" class="cn.edu.jxust.pojo.Address" /><!-- 第一种,普通值注入,value--><bean id="student" class="cn.edu.jxust.pojo.Student"><property name="name" value="浔阳江头月送客,枫叶荻花秋瑟瑟" /><!-- 第二种,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>听歌</value><value>敲代码</value><value>看电影</value></list></property><!-- Map--><property name="card"><map><entry key="身份证" value="361127200000000000"/><entry key="银行卡" value="555555555555555555" /></map></property><!-- set注入--><property name="game"><set><value>LOL</value><value>BOB</value><value>COC</value></set></property><!-- null注入--><property name="wife"><null /></property><property name="info"><props><prop key="学号">57200181636</prop><prop key="性别">男</prop></props></property></bean></beans>
2)pojo包
package cn.edu.jxust.pojo;public class Address {private String address;public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}@Overridepublic String toString() {return "Address{" +"address='" + address + '\'' +'}';}}
package cn.edu.jxust.pojo;import java.util.*;public class Student {private String name;private Address address;private String[] books;private List<String> hobbys;private Map<String,String> card;private Set<String> game;private String wife;private Properties 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> getGame() {return game;}public void setGame(Set<String> game) {this.game = game;}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;}@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", address=" + address.toString() +", books=" + Arrays.toString(books) +", hobbys=" + hobbys +", card=" + card +", game=" + game +", wife='" + wife + '\'' +", info=" + info +'}';}}
3)测试
import cn.edu.jxust.pojo.Student;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class MyTest {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");Student student = (Student) context.getBean("student");System.out.println(student.toString());/*Student{name='浔阳江头月送客,枫叶荻花秋瑟瑟',address=Address{address='null'},books=[红楼梦, 西游记, 三国, 三国演义],hobbys=[听歌, 敲代码, 看电影],card={身份证=361127200000000000,银行卡=555555555555555555},game=[LOL, BOB, COC],wife='null',info={学号=57200181636,性别=男}}*/}}
4)p命名和c命名注入
4.1 p命名(运用无参)
P命名空间注入 : 需要在头文件中加入约束文件 导入约束 : xmlns:p=”http://www.springframework.org/schema/p”
User类
package cn.edu.jxust.pojo;public class User {private String name;private int age;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}@Overridepublic String toString() {return "User{" +"name='" + name + '\'' +", age=" + age +'}';}}
userbeans.xml
<?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:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd"><!--P(属性: properties)命名空间 , 属性依然要设置set方法--><bean id="user" class="cn.edu.jxust.pojo.User" p:name="杨辉" p:age="18" /></beans>
4.2 c命名(运用有参)
c 命名空间注入 : 需要在头文件中加入约束文件 导入约束 : xmlns:c=”http://www.springframework.org/schema/c”
User类
package cn.edu.jxust.pojo;public class User {private String name;private int age;public User(){}//与p不同,多了有参构造函数public User(String name, int age){this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}@Overridepublic String toString() {return "User{" +"name='" + name + '\'' +", age=" + age +'}';}}
userbeans.xml
<?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:p="http://www.springframework.org/schema/p"xmlns:c="http://www.springframework.org/schema/c"xsi:schemaLocation="http://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd"><!--P(属性: properties)命名空间 , 属性依然要设置set方法--><bean id="user" class="cn.edu.jxust.pojo.User" p:name="杨辉" p:age="18" /><!--C(构造: Constructor)命名空间 , 属性依然要设置set方法--><bean id="user2" class="cn.edu.jxust.pojo.User" c:name="杨杨" c:age="19" /></beans>
4.3 MyTest测试
import cn.edu.jxust.pojo.Student;import cn.edu.jxust.pojo.User;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class MyTest {@Testpublic void Test(){ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");//p命名操作User user = (User) context.getBean("user");//c命名操作User user = (User) context.getBean("user2");System.out.println(user.toString());}}
5)Bean的作用域
5.1 单例模式(spring默认机制)
<bean id="ServiceImpl" class="cn.csdn.service.ServiceImpl" scope="singleton">
5.2 原型模式
每次从容器中get的时候,都会产生一个新对象!
<bean id="accountService" class="com.something.DefaultAccountService" scope="prototype"
