IOC和DI
IOC: Inversion of Control 控制反转. 指的是对象的创建权反转(交给)给Spring.作用是实现了程序的解耦合
DI: Dependency Injection 依赖注入.需要有IOC的环境,Spring创建这个类的过程中,Spring将类的依赖的属性设置进去
配置文件详解
- bean元素
- id: 给Bean起个名字 不能重复,不能使用特殊字符
- name:给Bean起个名字 能重复,能使用特殊字符
- class:类的完整类名
- 生命周期属性
- init-method 指出初始化方法
- destory-method 指出销毁方法
- 作用范围
- scope:
- singleton(默认值):单例.创建容器时会立即创建单例对象
- prototype :多例.每次获得对象时,才会创建对象,并且每次都会创建新的对象
- scope:
- 分模块开发
<import />
scope属性:Bean的作用范围
- singleton: 默认值,单例的.
- prototype: 多例的
- request: WEB项目中,Spring创建一个Bean的对象,将对象存到request域中.
- session: WEB项目中,Spring创建一个Bean的对象,将对象存入到session域中.
- globalSession: WEB项目中,应用在Porlet环境.如果没有Porlet环境那么globalSession相当于session.
Bean的生命周期的配置
通过配置标签上的init-method
作为Bean的初始化的时候执行的方法,配置destroy-method
作为Bean的销毁的时候执行的方法。
销毁方法想要执行,需要是单例创建的Bean而且在工厂关闭的时候,Bean才会被销毁
Spring的Bean的属性注入:
1.构造方法的方式注入属性
<bean id="car" class="spring.demo.Car">
<constructor-arg name="name" value="宝马"/>
<constructor-arg name="price" value="1000"/>
</bean>
2.set方法的方式注入:
<bean id="car2" class="spring.demo.Car">
<property name="name" value="宝马"/>
<property name="price" value="1000"/>
</bean>
Spring的属性注入
1.注入对象类型:
<bean id="car" class="spring.demo.Car">
<property name="name" value="宝马"/>
<!-- ref属性:引用另一个bean的id或name -->
<property name="car2" ref="car2"/>
</bean>
2.注入复杂类型
- 数组
- List
- Map集合
- Properties
<bean id="collectionBean"class="spring.demo.CollectionBean">
<!--数组类型的属性-->
<property name="arrs">
<list>
<value>v1</value>
<value>v2</value>
<value>v3</value>
</list>
</property>
<!--注入List集合的数据-->
<propertyname="list">
<list>
<value>list1</value>
<value>list2</value>
<value>list3</value>
</list>
</property>
<!--注入Map集合-->
<property name="map">
<map>
<entry key="aaa" value="111"/>
<entry key="bbb" value="222"/>
<entry key="ccc" value="333"/>
</map>
</property>
<!--Properties的注入-->
<property name="properties">
<props>
<prop key="username">root</prop>
<prop key="password">123</prop>
</props>
</property>
</bean>
配置注解扫描
Spring的注解开发:组件扫描(类上注解: 可以直接使用属性注入的注解)
<context:component-scan base-package="com.spring.demo1"/>
会扫描包下的所有子包
Spring的Bean管理的中常用的注解:
@Component:组件.(作用在类上)
Spring中提供@Component
的三个衍生注解:(功能目前来讲是一致的)
* @Controller:WEB层
* @Service:业务层
* @Repository:持久层
这三个注解是为了让标注类本身的用途清晰
属性注入的注解:(使用注解注入的方式,可以不用提供set方法.)
- Value:用于注入普通类型.
@Autowired:自动装配:
- 默认按类型进行装配.
- 按名称注入:
- @Qualifier:强制使用名称注入.
@Resource相当于:
- @Autowired和@Qualifier一起使用.
在POJO类中使用注解
Value例子:
//方式一:通过反射的Field赋值,破坏了封装性
@Value("tom")
private String name;
//方式二:通过set方法赋值,推荐使用
@Value("tom")
public void setName(String name){
this.name = name;
}
Autowired例子:
//方式一:
@Autowired //自动装配
private Car car;
//方式二:
@Autowired
//如果匹配多个类型一致的对象,将无法选择具体注入哪个对象
@Qualifier("car2")
private Car car;
Resource例子:
@Resource(name="car")//手动选择指定注入哪个对象
private Car car;
Bean的作用范围的注解
- @Scope:
- singleton:单例
- prototype:多例
Bean的生命周期的配置
- @PostConstruct: 相当于init-method
- @PreDestroy: 相当于destroy-method
例子:
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import javax.xml.ws.RespectBinding;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
//<bean name="user" class="bean.User" />
//@Component("user")
// @Service("user") // service层
// @Controller("user") // web层
@Repository("user")// dao层
//指定对象的作用范围
@Scope(scopeName="singleton")
public class User {
private String name;
@Value("18")
private Integer age;
//@Autowired //自动装配
//问题:如果匹配多个类型一致的对象.将无法选择具体注入哪一个对象.
//@Qualifier("car2")//使用@Qualifier注解告诉spring容器自动装配哪个名称的对象
@Resource(name="car")//手动注入,指定注入哪个名称的对象
private Car car;
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
public String getName() {
return name;
}
@Value("tom")
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@PostConstruct //在对象被创建后调用.init-method
public void init(){
System.out.println("我是初始化方法!");
}
@PreDestroy //在销毁之前调用.destory-method
public void destory(){
System.out.println("我是销毁方法!");
}
@Override
public String toString() {
return "User [name=" + name + ", age=" + age + ", car=" + car + "]";
}
}
spring与Junit整合测试
1.导包
需要额外添加aop
和test
包
- spring-aop-4.2.4.RELEASE.jar
- spring-test-4.2.4.RELEASE.jar
2.配置注解
新建一个测试类
//创建容器
@RunWith(SpringJUnit4ClassRunner.class)
//指定创建容器时使用哪个配置文件
@ContextConfiguration("classpath:config/applicationContext.xml")
public class Demo {
//将名为user的对象注入到u变量中
@Resource(name="user")
private User u;
3. 测试
@Test
public void fun1(){
System.out.println(u);
}