11-24 程宗武
11-24 Spring02
1 基于注解的 IOC 配置
之前我们是在xml中配置资源,现在我们也可以使用注解的方式来代替xml配bean的操作,这两者实现的功能都是一样的
基于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" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 业务层的配置-->
<bean id="service" class="top.wutiaojian.spring.service.impl.AccountServiceImpl">
<property name="accountDao" ref="dao"></property>
</bean>
<!-- 持久层Bean的配置-->
<bean id="dao" class="top.wutiaojian.spring.dao.impl.AccountDaoImpl">
<property name="runner" ref="runner"></property>
</bean>
<bean id="runner" class="org.apache.commons.dbutils.QueryRunner">
<constructor-arg name="ds" ref="dataSource"></constructor-arg>
</bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://192.168.66.128/mybatis"></property>
<property name="user" value="root"></property>
<property name="password" value="123456"></property>
</bean>
</beans>
基于注解的 IOC 配置
1.1 添加环境依赖
<properties>
<spring.version>5.0.2.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- db -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.2.1</version>
</dependency>
<!-- 日志 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
<!-- 数据库工具类 -->
<!-- https://mvnrepository.com/artifact/commons-dbutils/commons-dbutils -->
<dependency>
<groupId>commons-dbutils</groupId>
<artifactId>commons-dbutils</artifactId>
<version>1.6</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.2.RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
1.2 xml配置中开启对注解的支持
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置创建容器时要扫描那些包-->
<context:component-scan base-package="top.wutiaojian"/>
<!-- 配置Runner ,因为每次处理事务都有可能是不一样的,因此我们需要不同的Runner-->
<bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype">
<constructor-arg index="0" ref="dataSource"></constructor-arg>
</bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://192.168.66.128/mybatis"></property>
<property name="user" value="root"></property>
<property name="password" value="123456"></property>
</bean>
</beans>
1.3 使用注解配置管理的资源
1.3.1 用于创建对象的
@Component("accountDao")
//当我们使用注解的方式配置管理的资源时,类中需要配置的属性可以不用再写set方法
public class AccountDaoImpl implements IAccountDao {
@Component 注解的作用类似于xml中的 配
的操作,都是把资源交给Spring来管理 @Component注解还可以替换为@Controller @Service @Repository 他们的作用都是一样的,都是让Spring创建对象,只是语义化更加明确,
括号内的value值类似于bean标签中的id属性 ,如果不指定,那么默认值为当前首字母小写的类名
持久层
@Repository("accountDao")
public class AccountDaoImpl implements IAccountDao {
@Resource
private QueryRunner runner;
//省略了其中的方法
}
业务层
@Service("accountService")
public class AccountServiceImpl implements IAccountService {
@Resource(name = "accountDao")
private IAccountDao accountDao;
//省略了其中的方法
}
实体类
@Data
public class Account implements Serializable {
private Integer id;
private String name;
private Double money;
}
1.3.2 用于注入数据的
用于注入数据的有@Autowired @Qualifier @Resource @Value(用于注入基本数据类型和String类型)
@Autowired @Qualifier 这两个一般是搭配使用的
@Autowired
@Qualifier("accountDao")
private IAccountDao accountDao;
/*
1.@Autowired
如果只写@Autowired注解的话,一般会先根据要注入数据的类型查找,如果有两个相同类型(两个实现类实现的同一个接口)则会默认将属性的变量名作为id进行查找,如果没有找到就会报错,不过可以设置@Autowired的value值为(required=false)此时允许要注入的数据可以不存在
2.@Qualifier
这个注解一般是配合@Autowired注解进行使用,因为这个注解是在有了按照类型匹配的基础上再根据id进行匹配的,
不过可以单独的给方法注入参数
*/
@Resource
@Resource
private IAccountDao accountDao;
/*
@Resource装配顺序
1. 如果同时指定了name和type,则从Spring上下文中找到唯一匹配的bean进行装配,找不到则抛
出异常
2. 如果指定了name,则从上下文中查找名称(id)匹配的bean进行装配,找不到则抛出异常
3. 如果指定了type,则从上下文中找到类型匹配的唯一bean进行装配,找不到或者找到多个,都会
抛出异常
4. 如果既没有指定name,又没有指定type,则自动按照byName方式进行装配;如果没有匹配,则
回退为一个原始类型进行匹配,如果匹配则自动装配;
*/
1.3.3 用于改变作用范围的
@Scope value的取值有singleton prototype request session globalsession
@Service("accountService")
@Scope(value = "prototype")
public class AccountServiceImpl implements IAccountService
2.使用注解替代xml的配
1.编写配置类
数据库配置文件 db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.66.128/mybatis
jdbc.username=root
jdbc.password=123456
@Configuration //声明这个类是一个配置类当Spring创建容器时会在该类上加载注解
@ComponentScan("top.wutiaojian.spring") //告诉Spring需要扫描那些包
@PropertySource("db.properties") //加载配置文件的注解
public class SpringConfig {
@Value("${jdbc.driver}") //用于指定基本数据类型或String类型的属性的值
private String driver;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Bean(name = "dataSource")//这个注解表明这个方法会创建一个对象并放入Spring的容器之中(只能写在方法上)
public DataSource createDataSource() {
ComboPooledDataSource source = new ComboPooledDataSource();
try {
source.setDriverClass(driver);
source.setJdbcUrl(url);
source.setUser(username);
source.setPassword(password);
return source;
} catch (PropertyVetoException e) {
e.printStackTrace();
}
return null;
}
@Bean(name = "runner")
public QueryRunner getRunner(DataSource source) {
return new QueryRunner(source);
}
}
这里我们如果要使用其他配置类可以使用@Import标签进行导入 @Import({xxx.class})
3.Spring整合Junit
3.1 导入依赖的jar包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.2.RELEASE</version>
<scope>test</scope>
</dependency>
3.2 添加相应的注解
注 这里要求Junit为4.12以上的版本
@RunWith(SpringJUnit4ClassRunner.class) //使用Spring的运行器
//@ContextConfiguration(locations = {"classpath:spring02.xml"}) 如果需要加载配置文件的话可以使用这个 classpath为类的根路径
@ContextConfiguration(classes = SpringConfig.class)//指定相应的配置类
public class AopTest {
@Autowired //注入相关资源
private IAccountService service;
近期评论