Spring(04):依赖注入
概念(Dependency Injection)
- 依赖注入(Dependency Injection,DI)
- 依赖 : 指Bean对象的创建依赖于容器 . Bean对象的依赖资源
- 注入 : 指Bean对象所依赖的资源 , 由容器来设置和装配
Set 注入 (重点)
被注入的属性 , 必须有set方法 , set方法的方法名由set + 属性首字母大写 , 如果属性是boolean类型 , 没有set方法 , 是 is
代码
-
实体类
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 into; 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 getInto() { return into; } public void setInto(Properties into) { this.into = into; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", address=" + address + ", books=" + Arrays.toString(books) + ", hobbys=" + hobbys + ", card=" + card + ", games=" + games + ", wife='" + wife + '\'' + ", into=" + into + '}'; } }
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 + '\'' + '}'; } }
-
配置.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/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="address" class="com.gener.pojo.Address"> <property name="address" value="天津"/> </bean> <bean id="student" class="com.gener.pojo.Student"> <!--普通值注入,value--> <property name="name" value="Gener"/> <!--Bean注入,ref--> <property name="address" ref="address"/> <!--数组--> <property name="books"> <array> <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="1231312313123123213"/> <entry key="银行卡" value="6767667667767766666"/> </map> </property> <!--Set--> <property name="games"> <set> <value>英雄联盟</value> <value>王者荣耀</value> <value>和平精英</value> </set> </property> <!--null--> <property name="wife"> <null/> </property> <!--Properties--> <property name="into"> <props> <prop key="driver">xxxxx</prop> <prop key="url">xxxxx</prop> <prop key="username">xxxxx</prop> <prop key="password">xxxxx</prop> </props> </property> </bean> </beans>
-
测试
public class MyText { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); Student student = (Student) context.getBean("student"); System.out.println(student.toString()); } }
P命名空间注入
P(属性: properties)命名空间 , 属性依然要设置set方法
代码
-
创建实体类
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; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
-
配置.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/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="user" class="com.gener.pojo.User" p:age="23" p:name="gener"/> <!-- P命名空间需要添加的约定文件: xmlns:p="http://www.springframework.org/schema/p" --> </beans>
-
测试
@Test public void test(){ ApplicationContext context = new ClassPathXmlApplicationContext("userBeans.xml"); User user = context.getBean("user", User.class); System.out.println(user); }
C命名空间注入
C(构造: Constructor)命名空间 , 属性依然要设置set方法
代码
-
创建实体类
public class User { private String name; private int age; public User() { } //这里有有参构造 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; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
-
配合.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:c="http://www.springframework.org/schema/c" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="user2" class="com.gener.pojo.User" c:age="24" c:name="小鸡儿"/> <!-- C命名空间需要添加的约定文件: xmlns:c="http://www.springframework.org/schema/c" --> </beans>
-
测试
@Test public void test(){ ApplicationContext context = new ClassPathXmlApplicationContext("userBeans.xml"); User user = context.getBean("user2", User.class); System.out.println(user); }
bean的作用域
-
单例模式(Spring默认)
<bean id="user2" class="com.gener.pojo.User" c:age="24" c:name="小鸡儿" scope="singleton"/>
-
原型模式(每次从容器中get时,都会产生一个新的对象)
<bean id="user2" class="com.gener.pojo.User" c:age="24" c:name="小鸡儿" scope="prototype"/>
-
剩下3种只能在web开发中使用到;
B站学习狂神说Spring的笔记==>视频入口