spring基本配置与自动装配实现
spring基本配置与自动装配实现
spring xml基础配置
<--spring配置-->
<?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">
</beans>
<!--
id:bean的唯一标识符,也就是相当于我们学的对象名
class:bean对象所对应的权限定名:包名+类型
name:也是别名,而且name可以同时取多个别名
-->
<bean id="userT" class="com.xx.pojo.UserT" name="t,u2" >
</bean>
<!--这个import,一般用于团队开发使用,他可以将多个配置文件,导入合并为一个
假设,现在项目中有多个人开发,这三个人复制不同的类开发,不同的类需要注册在不同的bean中,
我们可以利用import将所有人的beans.xml合并成一个总的。
-->
<import resource="beans.xml"/>
<import resource="beans2.xml"/>
<import resource="beans3.xml"/>
- beans.xml
- beans2.xml
- beans3.xml
- applicationcontext.xml
DI依赖注入
构造器注入
Set方式注入
- 依赖注入:Set注入!
- 依赖:bean对象的创建依赖于容器
- 注入:bean对象中的所有属性,由容器来注入!
【环境搭建】
-
复杂类型
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 Listhobbys;
private Map<String,String> card;
private Setgame;
private Properties info;
private String wife;
} -
beans.xml
-
测试类
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.getAddress());
}
}
完善注入信息
<?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.abc.pojo.Address">
<property name="address" value="Shanghai"/>
</bean>
<!--第一种,普通值注入,value-->
<bean id="student" class="com.abc.pojo.Student">
<property name="name" value="cc"/>
<!--第二种,Bean注入,ref-->
<property name="address" ref="address"/>
<!--数组-->
<property name="books">
<array>
<value>三国演义</value>
<value>西游记</value>
<value>水浒传</value>
<value>红楼梦</value>
</array>
</property>
<!--List-->
<property name="hobbys">
<list>
<value>唱</value>
<value>跳</value>
<value>rap</value>
<value>篮球</value>
</list>
</property>
<!--Map-->
<property name="card">
<map>
<entry key="身份证" value="1265165"/>
<entry key="卡号" value="468489"/>
</map>
</property>
<!--Set-->
<property name="game">
<set>
<value>LOL</value>
<value>COC</value>
<value>BOB</value>
</set>
</property>
<!--null-->
<property name="wife">
<null></null>
</property>
<!--Properties-->
<property name="info">
<props>
<prop key="driver">15615xx</prop>
<prop key="url">1wb5bw3</prop>
<prop key="username">root</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.abc.pojo.User" p:name="cyh" p:age="18"/>
<!--c命名空间注入,通过构造器注入:construct-args-->
<bean id="user2" class="com.abc.pojo.User" c:age="18" c:name="ks"/>
</beans>
测试:
@Test
public void test2(){
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的作用域:
-
单例模式(Spring默认机制)
-
原型模式(每次从容器中get的时候,都会产生一个新对象!)
-
其余的request,session,application,这些只能在web开发中使用
Bean的自动装配
- 自动装配是Spring满足bean依赖的一种方式!
- Spring会在上下文中自动寻找,并自动给bean装配属性
在Spring有三种装配方式
- 在xml中显式的配置
- 在java中显式的配置
- 隐式的自动装配bean【重要】
测试
环境搭建:一个人有一猫一狗两只宠物
byName自动装配
<bean id="dog" class="com.aa.pojo.Dog"/>
<bean id="cat" class="com.aa.pojo.Cat"/>
<!--byName:会自动在容器上下文查找,和自己对象set方法后面的值对应的beanid!
-->
<bean id="people" class="com.aa.pojo.People" autowire="byName">
<property name="name" value="xxhh"/>
</bean>
</beans>
byType自动装配
<bean class="com.aa.pojo.Dog"/>
<bean class="com.aa.pojo.Cat"/>
<!-- byType:会自动在容器上下文查找,和自己对象属性类型相同的bean-->
<bean id="people" class="com.aa.pojo.People" autowire="byType">
<property name="name" value="xxhh"/>
</bean>
</beans>
小结:
- byName的时候,需要保证所有bean的id唯一,并且这个bean需要和自动注入属性的set方法的值一致
- byType的时候,需要保证所有bean的class唯一,并且这个bean需要和自动注入属性的类型一致
使用注解实现自动装配
要使用注解须知:
-
导入约束: xmlns:context="http://www.springframework.org/schema/context"
-
配置注解的支持: context:annotation-config/
@Autowired
直接在属性上使用即可!也可以在set方式上使用!
使用Autowired我们可以不用编写Set方法,前提是你这个自动装配的属性在IOC(Spring)容器中存在,且符合名字byname
@Nullable 字段标记了这个注解,说明这个字段可以为null;
public @interface Autowired {
boolean required() default true;
}
测试代码:
public class People {
//如果显式定义Autowired的required属性为false,说明这个对象可以为null,否则不允许为空。
@Autowired(required = false)
private Dog dog;
@Autowired
private Cat cat;
private String name;
}
如果@Autowired自动装配的环境比较复杂,自动装配无法通过一个注解【@Autowired】完成时,我们可以使用@Qualifier(value = "xxx")去配置@Autowired的使用,指定一个唯一的bean对象注入
public class People {
@Autowired
@Qualifier(value = "dog222")
private Dog dog;
@Autowired
@Qualifier(value = "cat122")
private Cat cat;
}
@Resource注解
public class People {
@Resource
private Dog dog;
@Resource(name="cat22")
private Cat cat;
}
小结:
@Resource和@Autowired的区别:
- 都是用来自动装配的,都可以放在属性字段上
- @Autowired通过byType的方式实现,而且必须要求这个对象存在!
- @Resource默认通过byname的方式实现,如果找不到名字,则通过byType实现。如果两个都找不到的情况下,则报错。【常用】
- 执行顺序不同:@Autowired通过byType的方式实现。@Resource默认通过byname的方式实现

浙公网安备 33010602011771号