6、依赖注入
6.1 构造器注入(参考第三节)
6.2 Set注入
- 依赖注入 :set注入
- 依赖注入
- 依赖 :bean对象的创建依赖于容器
- 注入 :bean对象中的所有属性 由容器注入
- 编写实体类
//实体类一
package pojo;
public class Address {
private String address;
public Address(){
}
public Address(String address) {
this.address = address;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Address{" +
"address='" + address + '\'' +
'}';
}
}
//实体类二
package pojo;
import java.util.*;
public class Student {
private Address address;
private String name ;
private String[] books;
private List<String> hobby;
private Set<String> games;
private Map<String,Integer > score;
private Properties info;
/*构造函数*/
public Student(){
}
public Student(Address address, String name, String[] books, List<String> hobby, Set<String> games, Map<String, Integer> score, Properties info) {
this.address = address;
this.name = name;
this.books = books;
this.hobby = hobby;
this.games = games;
this.score = score;
this.info = info;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String[] getBooks() {
return books;
}
public void setBooks(String[] books) {
this.books = books;
}
public List<String> getHobby() {
return hobby;
}
public void setHobby(List<String> hobby) {
this.hobby = hobby;
}
public Set<String> getGames() {
return games;
}
public void setGames(Set<String> games) {
this.games = games;
}
public Map<String, Integer> getScore() {
return score;
}
public void setScore(Map<String, Integer> score) {
this.score = score;
}
public Properties getInfo() {
return info;
}
public void setInfo(Properties info) {
this.info = info;
}
@Override
public String toString() {
return "Student{" +
"address=" + address +
", name='" + name + '\'' +
", books=" + Arrays.toString(books) +
", hobby=" + hobby +
", games=" + games +
", score=" + score +
", info=" + info +
'}';
}
}
- 编写beans.xml文件 注意一下map和props
<?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
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--注册bean-->
<bean id="address" class="pojo.Address">
<property name="address" value="小李"/>
</bean>
<bean id="student" class="pojo.Student">
<property name="address" ref="address"/>
<property name="name" value="12"/>
<property name="books">
<array>
<value>三国</value>
<value>三国</value>
<value>三国</value>
</array>
</property>
<property name="games">
<list>
<value>112</value>
<value>112</value>
</list>
</property>
<property name="hobby">
<list>
<value>1</value>
<value>2</value>
</list>
</property>
<property name="score">
<map>
<entry key="语文" value="90"/>
<entry key="数学" value="90"/>
</map>
</property>
<property name="info">
<props>
<prop key="1">2</prop>
</props>
</property>
</bean>
</beans>
- 测试
package pojo;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
@Test
public void test(){
/*获取容器*/
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
/*从容器里面取出对象*/
Student student = context.getBean("student", Student.class);
/*输出*/
System.out.println(student);
}
}
6.3 拓展
- p命名空间注入 set注入
- 第一步 编写实体类
package pojo;
public class User {
private String name ;
private String password;
public User(){
}
public User(String name, String password) {
this.name = name;
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", password='" + password + '\'' +
'}';
}
}
- 第二步 编写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
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--需要导入约束-->
<!--注册bean-->
<bean id="user" class="pojo.User" p:name="lsl" p:password="123456"/>
</beans>
- 测试
package pojo;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
@Test
public void test1(){
ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
User user = context.getBean("user", User.class);
System.out.println(user);
}
}
-
c命名空间注入 构造器注入 代码与上面的p命名空间注入相似
-
总结 :这两种方式都不能直接使用 需要导入约束
6.4 bean的作用域
6.4.1 Singleton Scope
- 只有一个单例 Bean 的共享实例被管理,所有对具有符合该Bean定义的ID的Bean的请求都会被Spring容器返回该特定的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"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--注册bean-->
<bean id="use" class="pojo.User" c:_0="1" c:_1="2" scope="singleton"/>
</beans>
6.4.2 Prototype Scope
- Bean 部署的非 singleton prototype scope 导致每次对该特定Bean的请求都会创建一个新的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"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--注册bean-->
<bean id="use" class="pojo.User" c:_0="1" c:_1="2" scope="prototype"/>
</beans>
标签:依赖,return,String,Sping,void,address,注入,public,name
From: https://www.cnblogs.com/advancingSnail/p/18159255