DI依赖注入(Dependency Injection,DI)就是IOC的一个实现,spring通过DI向javaBean(java类)注入属性
依赖:指javaBean的对象创建依赖于Spring容器
注入:指javaBean对象依赖的资源
构造器注入
public class User{
private User(int id,String name){
this.id = id;
this.name = name;
}
private int id;
private String name;
}
<?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 id="user" class="com.znsd.spring.vo.UserVO">
<!--根据参数名字-->
<constructor-arg name="id" value="1"/>
<constructor-arg name="name" value="刘志辉"/>
<!--根据下标名字-->
<constructor-arg name="0" value="1"/>
<constructor-arg name="1" value="刘志辉"/>
<!--根据参数类型-->
<constructor-arg name="java.lang.Integer" value="1"/>
<constructor-arg name="java.lang.String" value="刘志辉"/>
</bean>
</beans>
setter注入 【重点】
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;
public String getNameString() {
return nameString;
}
public void setNameString(String nameString) {
this.nameString = nameString;
}
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;
}
@Override
public String toString() {
return "Student [nameString=" + nameString + ", address=" + address + ", books=" + Arrays.toString(books)
+ ", hobbys=" + hobbys + ", card=" + card + ", games=" + games + ", wife=" + wife + ", info=" + info
+ "]";
}
}
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="address" class="com.znsd.spring.vo.Address"></bean>
<bean id="student" class="com.znsd.spring.vo.Student">
<property name="name" value="刘志辉"></property>
<!-- 常量注入 -->
<property name="address" ref="address"></property>
<!-- 数组注入 -->
<property name="books">
<list>
<value>book1</value>
<value>book2</value>
<value>book3</value>
<value>book4</value>
</list>
</property>
<!-- 集合注入 -->
<property name="hobbys">
<list>
<value>吃</value>
<value>喝</value>
<value>玩</value>
<value>乐</value>
</list>
</property>
<!-- map注入 -->
<property name="card">
<map>
<entry key="1" value="one"></entry>
<entry key="2" value="two"></entry>
<entry key="3" value="three"></entry>
<entry key="3" value="four"></entry>
</map>
</property>
<!-- set注入 -->
<property name="games">
<set>
<value>求生之路</value>
<value>文明</value>
<value>欧陆风云</value>
</set>
</property>
<!-- null注入 -->
<property name="wife">
<null />
</property>
<!-- Properties注入 -->
<property name="info">
<props>
<prop key="one">INDIA</prop>
<prop key="two">Pakistan</prop>
<prop key="three">USA</prop>
</props>
</property>
</bean>
</beans>
class Test {
@org.junit.jupiter.api.Test
void test() {
ApplicationContext aContext = new ClassPathXmlApplicationContext("beans.xml");
Student student = aContext.getBean("student",Student.class);
System.out.println(student);
}
}
p命名和c命名注入
1.p标签注入
导入约束 : xmlns:p="http://www.springframework.org/schema/p"
<!--P(属性: properties)命名空间 , 属性依然要设置set方法-->
<bean id="user" class="com.znsd.vo.User" p:name="刘志辉" p:age="18"/>
2.c标签注入
导入约束 : xmlns:p="http://www.springframework.org/schema/p"
<!--C(构造: Constructor)命名空间 , 要设置有参构造-->
<bean id="user" class="com.znsd.vo.User" c:name="刘志辉" c:age="18"/>
在spring中声明一个bean时,你可以指定它的作用域(使用scope属性)
作用域 | 描述 |
---|---|
singleton | 在spring IOC容器中只存在一个Bean实例,Bean以单例的方式存在,默认 |
prototype | 每次从容器中调用Bean时,都会返回一个新的实例,每次调用getBean()时 |
request | 每一次HTTP请求都会创建一个新的Bean,适用于Web |
session | 同一个HTTP请求共享一个Bean,不同Session使用不同的Bean,适用有Web |
global-session | 一般用于Portlet应用环境,该作用域仅适用于Webt |
<bean id="student" class="com.znsd.spring.vo.Student" scope="singleton">
<property name="id" value="1"></property>
<property name="name" value="刘志辉"></property>
</bean>
Singleton测试
<bean id="student" class="com.znsd.spring.vo.Student" scope="prototype">
<property name="id" value="1"></property>
<property name="name" value="刘志辉"></property>
</bean>
prototype测试
更加详细的
原文:https://www.cnblogs.com/liuzhihui021221/p/14695732.html