Spring 有两个主要的作用一是IOC(控制反转和依赖注入)二是AOP(事务处理)
IOC :控制反转,这个反转是将对象的获取过程被反转了,之前的对象需要我们手动的建立,而在Spring中是由容器进行注入的,就像是买东西,我们之前需要一个工具(对象)都是自动手动建造这个工具,而现在我们是找一个店铺(Spring容器),然后根据告诉他们我们需要什么工具,他就会给我们一个这样的工具。
其中依赖注入是控制反转的主要实现方式
Spring自动注入有两种方式进行实现:
一、通过xml配置文件进行注入
二、通过注解的方式进行注入
我们先看一下通过xml的配置进行注入
注入分为手动注入和自动注入,
1)手动注入
手动注入就是在注册bean时手动写上注入的对象,例如:
<bean id="sayHelloService" class="com.lei.spring.service.SayHelloServiceImpl">
<property name="sayHelloService" ref="sayHelloDao"></property>
</bean>
2)自动注入
自动注入是不需要写<property name="sayHelloService" ref="sayHelloDao"></property> 这些的,
只需要在 <beans default-autowire="byName" > 声明一下即可
他由三种方式去自动注入 1 是byName 根据类名进行注入(默认是类名第一个字母变为小写为其名字)2 是byType 根据类型进行注入,也就是class属性,这时可以不用给bean设置id。3是 no (这是默认设置)
spring-bean.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd"
<bean id="sayHelloDao" class="com.lei.spring.dao.SayHelloDaoImpl"></bean>
<bean id="sayHelloService" class="com.lei.spring.service.SayHelloServiceImpl">
<!-- 通过设值注入 也就是setter方法注注入,getter方法可以没有 -->
<property name="sayHelloService" ref="sayHelloDao"></property>
<!-- 通过构造 方法注入 -->
<constructor-arg name="sayHelloService" ref="sayHelloDao"></constructor-arg>
</bean>
</beans>
java注入操作
package com.lei.spring.service;
import com.lei.spring.dao.SayHelloDao;
public class SayHelloServiceImpl implements SayHelloService {
private SayHelloDao sayHelloDao;
//通过设值注入Bean
public void setSayHelloDao(SayHelloDao sayHelloDao) {
this.sayHelloDao = sayHelloDao;
}
//通过构造方法注入bean
public SayHelloServiceImpl(SayHelloDao sayHelloDao) {
this.sayHelloDao = sayHelloDao;
}
@Override
public String say(String str) {
//模拟处理数据操作
str += ":已处理过数据";
return sayHelloDao.say(str);
}
}
我们再看一下通过注解的方式怎么实现注入Bean
spring-bean.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd"
<!-- 开启自动扫描注册Bean -->
<context:component-scan base-package="com.lei.spring"></context:component-scan>
</beans>
dao层
package com.lei.spring.dao;
import org.springframework.stereotype.Repository;
@Repository //告诉容器这是一个dao
public interface SayHelloDao {
public String say(String str);
}
service层
package com.lei.spring.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lei.spring.dao.SayHelloDao;
@Service //告诉容器这是一个service
public class SayHelloServiceImpl implements SayHelloService {
@Autowired //自动装配Bean
private SayHelloDao sayHelloDao;
@Override
public String say(String str) {
//模拟处理数据操作
str += ":已处理过数据";
return sayHelloDao.say(str);
}
}
注:
@Repository 对dao进行注解
@Service 对service进行注解
@Controller 对Controller进行注解
他们三个都实现了 @Component 的注解
@Autowired 自动注入,他可以标识在私有对象上,也可以标注在setter方法上或者是构造方法上
@Required 是指明这个属性必须要注入,不然会报错,
@Autowired(required=false) 是指明这个对象可以不用注入
默认情况下,Spring将把组件Class的第一个字母变成小写,来作为自动扫描组件的名称,例如将“SayHelloService”转变为“sayHelloService” 声明的时候就要写 private SayHelloService sayHelloService;
也可以手动给他们设置@Repository("myDao") 那么声明的时候就要写 private SayHelloDao myDao;