1. Spring配置
1.1 别名
<bean id="user" class="com.lu.pojo.User">
<constructor-arg name="name" value="秦时明月"></constructor-arg>
</bean>
<alias name="user" alias="newuser"></alias>
1.2 Bean的配置
<!-- id:bean的唯一标识符,也就是相当于我们学的对象名
class:bean对象所对应的全限定名:包名 + 类型
name:也是别名,而且name可以同时取多个别名-->
<bean id="user" class="com.lu.pojo.User" name="newuser2 newuser3">
<constructor-arg name="name" value="秦时明月"></constructor-arg>
</bean>
1.3 import
一般用于团队开发使用,他可以将多个配置文件,导入合并为一个
假设,现在项目中有多个人开发,这三个人负责不同的类开发,不同的类需要注册在不同的bean中,我们可以利用import将所有人的beans.xml合并为一个总的!
applicationContext.xml
<import resource="beans.xml"></import>
<import resource="beans2.xml"></import>
<import resource="beans3.xml"></import>
使用的时候,直接使用总的配置就可以了
2. 依赖注入
2.1 构造器注入
2.2 Set方式注入
依赖注入:Set注入!
- 依赖:bean对象的创建依赖于容器!
- 注入:bean对象的所有属性,由容器来注入!
【环境搭建】
1.复杂类型
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
2.真实测试环境
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 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 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 +
", games=" + games +
", wife='" + wife + '\'' +
", info=" + info +
'}';
}
}
3.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-3.0.xsd">
<bean id="student" class="com.lu.pojo.Student">
<property name="name" value="卢兴才"></property>
</bean>
</beans>
4.测试类
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());
}
}
5.完善注入信息
<?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-3.0.xsd">
<bean id="address" class="com.lu.pojo.Address">
<property name="address" value="青岛"></property>
</bean>
<bean id="student" class="com.lu.pojo.Student">
<!--第一种,普通注入,value-->
<property name="name" value="弋寒"></property>
<!--第二种,Bean注入,ref-->
<property name="address" ref="address"></property>
<!--数组注入,ref-->
<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="123456"></entry>
<entry key="银行卡" value="654321"></entry>
</map>
</property>
<!--Set-->
<property name="games">
<set>
<value>LOL</value>
<value>王者荣耀</value>
<value>COC</value>
</set>
</property>
<!--null-->
<property name="wife" value=""></property>
<!--Properties-->
<property name="info">
<props>
<prop key="学号">201707345</prop>
<prop key="性别">男</prop>
<prop key="姓名">小明</prop>
</props>
</property>
</bean>
</beans>
2.3 拓展方式注入
我们可以用p命名和c命名空间进行注入
官方解释:
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-3.0.xsd">
<!--p命名空间注入,可以直接注入属性的值:property-->
<bean id="user" class="com.lu.pojo.User" p:name="小明" p:age="15"></bean>
<!--c命名空间注入,通过构造器注入:construct-args-->
<bean id="user2" class="com.lu.pojo.User" c:age="18" c:name="小红"></bean>
</beans>
测试:
@Test
public void test2() {
ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
User user = (User)context.getBean("user2");
System.out.println(user.toString());
}
注意点:p命名和c命名空间不知能直接使用,需要导入xml约束
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
2.4 bean的作用域
1.单例模式(Spring的默认机制)
<bean id="user" class="com.lu.pojo.User" p:name="小明" p:age="15" scope="singleton"></bean>
2.原型模式;每次从容器中get的时候,都会产生一个新对象!
<bean id="accountService" class="com.something.DefaultAccountService" scope="prototype"/>
3.其余的request,session,application,这些个只能在web开发中使用到!