一、构造器注入
二、set方式注入
1.依赖注入:set注入
2.依赖:bean对象的创建依赖于容器
3.注入;bean对象中的所有属性,有容器来注入
4.环境搭建
① 复杂类型
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.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> 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;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address +
", books=" + Arrays.toString(books) +
", hobbys=" + hobbys +
", card=" + card +
", game=" + game +
", wife='" + wife + '\'' +
", info=" + info +
'}';
}
}
③ 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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="student" class="com.sun.pojo.Student">
<!--第一种 普同值注入value-->
<property name="name" value="小张雪"/>
</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.getName());
}
}
⑤ 完善注解
<?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="address" class="com.sun.pojo.Address"/>
<bean id="student" class="com.sun.pojo.Student">
<!--第一种,普同值注入value-->
<property name="name" value="小张雪"/>
<!--第二种,Bean注入:ref-->
<property name="address" ref="address"/>
<!--数组注入-->
<property name="books">
<array>
<value>java</value>
<value>c</value>
<value>python</value>
<value>c++</value>
</array>
</property>
<!--List注入-->
<property name="hobbys">
<list>
<value>听歌</value>
<value>刷抖音</value>
<value>敲代码</value>
</list>
</property>
<!--Map注入-->
<property name="card">
<map>
<entry key="身份证" value="2222222222222222"/>
<entry key="银行卡" value="1111111111111111"/>
</map>
</property>
<!--Set注入-->
<property name="game">
<set>
<value>王者</value>
<value>吃鸡</value>
<value>蛋仔</value>
</set>
</property>
<!--null注入-->
<property name="wife">
<null>/</null>
</property>
<!--Properties注入-->
<property name="info">
<props>
<prop key="driver">20240205</prop>
<prop key="url">男</prop>
<prop key="username">小明</prop>
<prop key="password">123456</prop>
</props>
</property>
</bean>
</beans>
三、其他方式注入
我们可以使用p命名空间和c命名空间进行注入
官方解释:
使用 :
<?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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--p:命名空间注入,可以直接注入属性的值:property-->
<bean id="user" class="com.sun.pojo.User" p:name="小张" p:age="18"/>
<!--c:命名空间注入,通过构造器注入:construct-args-->
<bean id="user2" class="com.sun.pojo.User" c:age="18" c:name="小张"/>
</beans>
测试:
@Test
public void text2(){
ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
User user = context.getBean("user", User.class);
System.out.println(user);
}
@Test
public void text3(){
ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
User user = context.getBean("user2", User.class);
System.out.println(user);
}
注意:p命名和c命名空间不能直接使用,需要导入xml约束
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
四、bean的作用域
Scope | Description |
singleton | (Default) Scopes a single bean definition to a single object instance for each Spring loC container. |
prototype | Scopes a single bean definition to any number of object instances. |
request | Scopes a single bean definition to the lifecycle of a single HTTP request. That is, each HTTP request has its own instance of a beancreated off the back of a single bean definition. Only valid in the context of a web-aware Spring ApplicationContext. |
session | Scopes a single bean definition to the lifecycle of an HTTP Session . Only valid in the context of a web-aware Spring ApplicationContext. |
application | Scopes a single bean definition to the lifecycle of a ServletContext . Only valid in the context of a web-aware Spring ApplicationContext. |
websocket | Scopes a single bean definition to the lifecycle of a WebSocket. Only valid in the context of a web-aware Spring ApplicationContext. |
1.单例模式(spring默认机制)
<bean id="user2" class="com.sun.pojo.User" c:age="18" c:name="小张" scope="singleton"/>
2.原型模式:每次从容器中get的时候,都会产生一个新对象
<bean id="user2" class="com.sun.pojo.User" c:age="18" c:name="小张" scope="prototype"/>
3.其余的request、session、application,这些个只能在web开发中使用到