文章目录
一、引言
1、写到此处
基于注解的 IoC 配置已经完成,但是大家都发现了一个问题:我们依然离不开 spring 的 xml 配置文件,那么能不能不写这个 bean.xml,所有配置都用注解来实现呢?
当然,同学们也需要注意一下,我们选择哪种配置的原则是简化开发和配置方便,而非追求某种技术。
2、待改造的问题
我们发现,之所以我们现在离不开 xml 配置文件,是因为我们有一句很关键的配置:
<!-- 告知spring框架在,读取配置文件,创建容器时,扫描注解,依据注解创建对象,并存入容器中 -->
<!-- 告知spring创建容器时要扫描的包-->
<context:component-scan base-package="com.itheima"></context:component-scan>
如果他要也能用注解配置,那么我们就离脱离 xml 文件又进了一步。
另外,数据源和 JdbcTemplate 的配置也需要靠注解来实现。
<!-- 配置QueryRunner -->
<bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype">
<!-- 注入数据源(构造方法注入) -->
<constructor-arg name="ds" ref="dataSource"></constructor-arg>
</bean>
<!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 连接数据库的必备信息 -->
<property name="driverClass" value="com.mysql.cj.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/eesy?useSSL=false&serverTimezone=UTC"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
</bean>
二、新注解说明
1、 @Configuration
作用:
用于指定当前类是一个 spring 配置类,当创建容器时会从该类上加载注解。获取容器时需要使用
AnnotationApplicationContext(有@Configuration 注解的类.class)。
属性: value:用于指定配置类的字节码
示例代码:
/**
* spring 的配置类,相当于 bean.xml 文件
*/
@Configuration
public class SpringConfiguration {
}
注意:
我们已经把配置文件用类来代替了,但是如何配置创建容器时要扫描的包呢?请看下一个注解。
2 、@ComponentScan
作用:
用于指定 spring 在初始化容器时要扫描的包。作用和在 spring 的 xml 配置文件中的:<context:component-scan base-package=“com.itheima”/>是一样的。
属性:
basePackages:用于指定要扫描的包。和该注解中的 value 属性作用一样。
示例代码:
/**
* spring 的配置类,相当于 bean.xml 文件
*/
@Configuration
@ComponentScan("com.itheima")
public class SpringConfiguration {
}
注意:
我们已经配置好了要扫描的包,但是数据源和 JdbcTemplate 对象如何从配置文件中移除呢?请看下一个注解。
3 、@Bean
作用:
该注解只能写在方法上,表明使用此方法创建一个对象,并且放入 spring 容器。
属性:
name:给当前@Bean 注解方法创建的对象指定一个名称(即 bean 的 id)。
示例代码:
/**
* 连接数据库的配置类
*/
public class JdbcConfig {
/**
* 创建一个数据源,并存入 spring 容器中
* @return
*/
@Bean(name="dataSource") //用于把当前方法的返回值作为bean对象存入spring的ioc容器中
public DataSource createDataSource() {
ComboPooledDataSource ds = new ComboPooledDataSource();
try {
ds.setDriverClass("com.mysql.cj.jdbc.Driver");
ds.setJdbcUrl("jdbc:mysql://localhost:3306/eesy?useSSL=false&serverTimezone=UTC");
ds.setUser("root");
ds.setPassword("root");
return ds;
} catch (PropertyVetoException e) {
throw new RuntimeException(e);
}
}
/**
* 用于创建QueryRunner对象
* @param dataSource
* @return
*/
@Bean(name="runner")//用于把当前方法的返回值作为bean对象存入spring的ioc容器中
@Scope("prototype")
public QueryRunner createQueryRunner(DataSource dataSource) {
return new QueryRunner(dataSource);
}
注意:
我们已经把数据源和runner 从配置文件中移除了,此时可以删除 bean.xml 了。但是由于没有了配置文件,创建数据源的配置又都写死在类中了。如何把它们配置出来呢?
请看下一个注解。
4、 @PropertySource
作用:
用于加载.properties 文件中的配置。例如我们配置数据源时,可以把连接数据库的信息写到properties 配置文件中,就可以使用此注解指定 properties 配置文件的位置。
属性:
value[]:用于指定 properties 文件位置。如果是在类路径下,需要写上 classpath:
示例代码:
配置:
/**
* 连接数据库的配置类
*/
public class JdbcConfig {
@Value("${jdbc.driver}")
private String driver;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
/**
* 创建一个数据源,并存入 spring 容器中
* @return
*/
@Bean(name="dataSource")
public DataSource createDataSource() {
try {
ComboPooledDataSource ds = new ComboPooledDataSource();
ds.setDriverClass(driver);
ds.setJdbcUrl(url);
ds.setUser(username);
ds.setPassword(password);
return ds;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
jdbc.properties 文件:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/day44_ee247_spring
jdbc.username=root
jdbc.password=1234
注意:
此时我们已经有了两个配置类,但是他们还没有关系。如何建立他们的关系呢?
请看下一个注解。
5、 @Import
作用:
用于导入其他配置类,在引入其他配置类时,可以不用再写@Configuration注解。当然,写上也没问题。
属性:
value[]:用于指定其他配置类的字节码。
示例代码:
@Configuration//可省略
@ComponentScan(basePackages = {"com.itheima"})
@Import(JdbcConfig.class)
public class SpringConfiguration {
}
@Configuration
@PropertySource("classpath:jdbc.properties")
public class JdbcConfig{
}
注意:
我们已经把要配置的都配置好了,但是新的问题产生了,由于没有配置文件了,如何获取容器呢?
请看下一小节。
6、通过注解获取容器:
ApplicationContext ac =new AnnotationConfigApplicationContext(SpringConfiguration.class);
三、优化案例
1、持久层
@Repository("accountDao")
public class AccountDao implements IAccountDao{
@Autowired
private QueryRunner runner;
public void setRunner(QueryRunner runner) {
this.runner = runner;
}
public List<Account> findAllAccount() {
try {
return runner.query("select * from account",new BeanListHandler<Account>(Account.class));
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public Account findAccountById(Integer id) {
try {
return runner.query("select * from account where id=?",new BeanHandler<Account>(Account.class),id);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public void saveAccount(Account account) {
try {
runner.update("insert into account(name,money) values(?,?)",account.getName(),account.getMoney());
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public void updateAccount(Account account) {
try {
runner.update("update account set name=?,money=? where id=?",account.getName(),account.getMoney(),account.getId());
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public void deleteAccount(Integer id) {
try {
runner.update("delete from account where id=?",id);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
2、业务层
/**
* 账户的业务层实现类
* @author
*
*/
@Service("accountService")
public class AccountServiceImpl implements IAccountService{
@Autowired
private IAccountDao accountDao;
public void setAccountDao(IAccountDao accountDao) {
this.accountDao = accountDao;
}
public List<Account> findAllAccount() {
return accountDao.findAllAccount();
}
public Account findAccountById(Integer id) {
return accountDao.findAccountById(id);
}
public void saveAccount(Account account) {
accountDao.saveAccount(account);
}
public void updateAccount(Account account) {
accountDao.updateAccount(account);
}
public void deleteAccount(Integer id) {
accountDao.deleteAccount(id);
}
}
3 、pom.xml配置文件
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-dbutils/commons-dbutils -->
<dependency>
<groupId>commons-dbutils</groupId>
<artifactId>commons-dbutils</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>
4、Spring配置文件
/**
* 该类是一个配置类,它的作用和bean.xml是一样的
* spring中的新注解
* 1、@Configuration和@ComponentScan
* @Configuration
* 作用:指定当前类是一个配置类
* 细节:
* 当(配置类)作为AnnotationConfigApplicationContext对象创建的参数时,该注解可以不写。
* ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class);
*
* @ComponentScan
* 作用:用于通过注解指定spring在创建容器时要扫描的包
* 属性:
* value:作用和basePackages一样,都是用于创建容器时要扫描的包。
* 使用此注解就等同于在xml中配置了
* <context:component-scan base-package="com.itheima"></context:component-scan>
*
* 2、bean注解
* 作用:用于把当前方法的返回值作为bean对象存入spring的ioc容器中
* 属性:
* name用于指定bean的id。默认值是当前方法的名称。
* id:方法名createQueryRunner
* value:返回值对象 new QueryRunner(dataSource)
* 细节:当我们使用注解配置方法时,如果方法有参数,spring框架会去容器中查找有没有可用的bean对象。
* ***查询的方式和Autowired注解的作用是一样的。
* 3、Import
* 作用:配置导入其他的配置类
* 属性:value
* 用于指定其他配置类的字节码。(子配置文件不用配置第一项11)
* 当我们使用Import的注解之后,有Import注解的类就是主配置,而导入的都是配置类。
*
*/
@Configuration//可省略
@ComponentScan(basePackages = {"com.itheima"})
@Import(JdbcConfig.class)
public class SpringConfiguration {
}
JdbcConfig 1
/**
* 和spring连接数据库相关的配置类
* @author
* 2、bean注解
* 作用:用于把当前方法的返回值作为bean对象存入spring的ioc容器中
* 属性:
* name用于指定bean的id。默认值是当前方法的名称。
* id:方法名createQueryRunner
* value:返回值对象 new QueryRunner(dataSource)
* 细节:当我们使用注解配置方法时,如果方法有参数,spring框架会去容器中查找有没有可用的bean对象。
* ***查询的方式和Autowired注解的作用是一样的。
*/
public class JdbcConfig {
/**
* 用于创建QueryRunner对象
* @param dataSource
* @return
*/
@Bean(name="runner")//用于把当前方法的返回值作为bean对象存入spring的ioc容器中
@Scope("prototype")
public QueryRunner createQueryRunner(DataSource dataSource) {
return new QueryRunner(dataSource);
}
/**
* 创建数据源对象
* @return
*/
@Bean(name="dataSource") //用于把当前方法的返回值作为bean对象存入spring的ioc容器中
public DataSource createDataSource() {
ComboPooledDataSource ds = new ComboPooledDataSource();
try {
ds.setDriverClass("com.mysql.cj.jdbc.Driver");
ds.setJdbcUrl("jdbc:mysql://localhost:3306/eesy?useSSL=false&serverTimezone=UTC");
ds.setUser("root");
ds.setPassword("root");
return ds;
} catch (PropertyVetoException e) {
throw new RuntimeException(e);
}
}
}
5、测试类
测试类1
public class AccountServiceTest {
@Test
public void testfindAll() {
//1、获取容器
//ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class);
//2、得到业务层对象
IAccountService as = (IAccountService) ac.getBean("accountService",IAccountService.class);
//3、执行方法
List<Account> accounts = as.findAllAccount();
for(Account account:accounts) {
System.out.println(account);
}
}
@Test
public void testfindOne() {
//1、获取容器
ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class);
IAccountService as = (IAccountService) ac.getBean("accountService");
Account account = as.findAccountById(2);
System.out.println(account);
}
@Test
public void testSaveAccount() {
ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class);
IAccountService as = (IAccountService) ac.getBean("accountService");
Account account = new Account();
account.setName("ddd");
account.setMoney(4000.0f);
as.saveAccount(account);
}
@Test
public void testUpdateAccount() {
ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class);
IAccountService as = (IAccountService) ac.getBean("accountService");
Account account = new Account();
account.setId(4);
account.setName("ddd");
account.setMoney(1000.0f);
as.updateAccount(account);
}
@Test
public void testDeleteAccount() {
ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class);
IAccountService as = (IAccountService) ac.getBean("accountService");
as.deleteAccount(8);
}
}
测试类2
/**
* 测试QueryRunner是否单例
* @author 宋强
*/
public class QueryRunnerTest {
@Test
public void testQueryRunner(){
ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class);
QueryRunner runner1 = ac.getBean("runner",QueryRunner.class);
QueryRunner runner2 = ac.getBean("runner",QueryRunner.class);
System.out.println(runner1==runner2);//prototype:false
}
//情况一:不用配置作用域 true是单例的
//情况二:配置@Scope("prototype")是多例的
}
测试类3
纯注解spring整合Junit
@RunWith(SpringJUnit4ClassRunner.class)
//@ContextConfiguration(locations = "classpath:bean.xml")
@ContextConfiguration(classes = SpringConfiguration.class)
public class AccountTest {
@Autowired
private AccountServiceImpl accountService;
/*private ApplicationContext ac;
private AccountServiceImpl accountService;
@Before
public void init(){
ac = new ClassPathXmlApplicationContext("bean.xml");
accountService = ac.getBean("accountService", AccountServiceImpl.class);
}*/
@Test
public void testFindAll(){
List<Account> all = accountService.findAll();
for (Account account : all) {
System.out.println(account);
}
}
}
四、分离SQL
1、jdbcConfig.properties
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/eesy?useSSL=false&serverTimezone=UTC
jdbc.username=root
jdbc.password=root
2、配置类
/**
* 该类是一个配置类,它的作用和bean.xml是一样的
* spring中的新注解
* 1、@Configuration和@ComponentScan
* @Configuration
* 作用:指定当前类是一个配置类
* 细节:当 (配置类) 作为AnnotationConfigApplicationContext对象创建的参数时,该注解可以不写。
* ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class);
* @ComponentScan
* 作用:用于通过注解指定spring在创建容器时要扫描的包
* 属性:
* value:作用和basePackages一样,都是用于创建容器时要扫描的包。
* 使用此注解就等同于在xml中配置了
* <context:component-scan base-package="com.itheima"></context:component-scan>
*
* 2、bean注解
* 作用:用于把当前方法的返回值作为bean对象存入spring的ioc容器中
* 属性:
* name用于指定bean的id。默认值是当前方法的名称。
* id:方法名createQueryRunner
value:返回值对象高new QueryRunner(dataSource)
细节:当我们使用注解配置方法时,如果方法有参数,spring框架会去容器中查找有没有可用的bean对象。
***查询的方式和Autowired注解的作用是一样的。
3、Import
作用:配置导入其他的配置类
属性:value
用于指定其他配置类的字节码。(子配置文件不用配置第一项)
当我们使用Import的注解之后,有Import注解的类就是主配置,而导入的都是配置类。
*
* 4、PropertySource
* 作用:用于指定properties文件的位置
* 属性:
* value:指定文件的名称和路径
* 关键字:classpath,表示类路径下
* 具有参数的方法:createQueryRunner(@Qualifier("ds2")DataSource dataSource)
*
* @Bean(name="ds2") public DataSource createDataSource2() {
* @Bean(name="ds1") public DataSource createDataSource() {
*
*/
@Configuration//可以省略
@ComponentScan(basePackages = {"com.itheima"})
@Import(JdbcConfig.class)
@PropertySource("classpath:jdbcConfig.properties")
public class SpringConfiguration {
}
3、配置类
/**
* 和spring连接数据库相关的配置类
* @author 宋强
*
*/
public class JdbcConfig {
@Value("${jdbc.driver}")
private String driver;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
/**
* 用于创建QueryRunner对象
* @param dataSource
* @return
*/
@Bean(name="runner")
@Scope("prototype")
public QueryRunner createQueryRunner(@Qualifier("ds2")DataSource dataSource) {
/**
* 先一上来按照类型注入,若没有或有多个类型,并且形参无法在多个匹配中找到符合名称的id时
* 此时@Qualifier的ds2起作用
*/
return new QueryRunner(dataSource);
}
/**
* 创建数据源对象
* @return
*/
@Bean(name="ds1")
public DataSource createDataSource() {
System.out.println("createDataSource1");
ComboPooledDataSource ds = new ComboPooledDataSource();
try {
ds.setDriverClass(driver);
ds.setJdbcUrl(url);
ds.setUser(username);
ds.setPassword(password);
return ds;
} catch (PropertyVetoException e) {
throw new RuntimeException(e);
}
}
@Bean(name="ds2")
public DataSource createDataSource2() {
System.out.println("createDataSource2");
ComboPooledDataSource ds2 = new ComboPooledDataSource();
try {
ds2.setDriverClass(driver);
ds2.setJdbcUrl(url);
ds2.setUser(username);
ds2.setPassword(password);
return ds2;
} catch (PropertyVetoException e) {
throw new RuntimeException(e);
}
}
}
4、测试类
public class AccountServiceTest {
@Test
public void testfindAll() {
//1、获取容器
//ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class);
//2、得到业务层对象
IAccountService as = (IAccountService) ac.getBean("accountService",IAccountService.class);
//3、执行方法
List<Account> accounts = as.findAllAccount();
for(Account account:accounts) {
System.out.println(account);
}
}
}