spring纯注解
开发中纯注解一般不现实 ,emm…
一、导入依赖
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.21</version>
</dependency>
</dependencies>
二、配置文件
1、jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/databaseName?useUnicode=true&characterEncoding=utf-8
jdbc.user=root
jdbc.password=root----->此处自己的数据库密码
2、忽略掉spring.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--读取properties文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--开启注解扫描-->
<context:component-scan base-package="com.ceshi"/>
<!--IOC-->
<bean id="accountController" class="com.ceshi.controller.AccountController">
<!--DI-->
<property name="accountService" ref="accountService"/>
</bean>
<bean id="accountService" class="com.ceshi.service.impl.AccountServiceImpl"></bean>
<!--导入第三方jar包-->
<bean id="dataSourse" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
三、实体类
public class Account {
private Integer id;
private String name;
private double money;
public Account(Integer id, String name, double money) {
this.id = id;
this.name = name;
this.money = money;
}
public Account() {
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + money +
'}';
}
//此处省略set和get方法
四、配置类
1、获取德鲁伊连接池
@PropertySource("jdbc.properties")
public class JdbcConfig {
@Value("${jdbc.driver}")
private String driverClassName;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.user}")
private String username;
@Value("${jdbc.password}")
private String password;
@Bean
public DruidDataSource createDataSource(){
//创建数据源对象并返回
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setDriverClassName(driverClassName);
druidDataSource.setUrl(url);
druidDataSource.setUsername(username);
druidDataSource.setPassword(password);
return druidDataSource;
}
}
2、配置入口类
/**
* 代表当前是配置入口类
* 扫描指定包下注解
*/
/**
配置类 作用替换掉spring.xml
如何标识这个类是一个配置类
注解 @Configuration
@ComponentScan("com.ceshi") 此注解可以替换掉
<context:component-scan base-package="com.ceshi"/>
*/
@Configuration
@ComponentScan("com.ceshi")
@Import(JdbcConfig.class)
public class SpringConfig {
}
五、service层
1、定义接口
public interface AccountService {
/**
*保存
*/
void save(Account account);
}
2、实现类
@Service
public class AccountServiceImpl implements AccountService{
public void save(Account account) {
System.out.println("保存用户信息成功:"+account);
}
}
六、controller层
@Controller
public class AccountController {
@Autowired
private AccountService accountService;
/*
spring替换掉
public void setAccountService(AccountService accountService) {
this.accountService = accountService;
}
*/
public void save(Account account){
accountService.save(account);
}
}
六、测试
测试1:
public class Test1 {
/**
* 1:IOC应用 以及 依赖注入应用
* 1.1: IOC 控制反转,就是把我们的对象交给spring管理,
* AccountController AccountService。
* 1.2 DI 依赖注入
* 我们发现 构建AccountController对象的时候,他有依赖的属性 AccountService 对象
* spring可以帮助我们在构建AccountController对象的时候,
* 完成 属性值的注入
* 这个过程称为依赖注入。
* 2:在测试类中完成 功能的调用 通过controler 调用save功能
*/
/*
* 普通测试
* */
@Test
public void test01(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
AccountController controller = context.getBean(AccountController.class);
Account account = new Account(1,"张三",9.1);
controller.save(account);
}
@Test
public void test02(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
DruidDataSource dataSource = context.getBean(DruidDataSource.class);
System.out.println(dataSource);
}
}
测试2:
public class Test2 {
/**
*
* 1: service controller 层纯注解开发。
*
* 2: 使用SpringConfig类替换掉spring核心配置文件,并完成注解包的扫描。
*
* 3: 创建纯注解的容器,完成测试。
*/
@Test
public void test02(){
// 创建 解析注解的容器类
// 测试的第一步 需要有容器对象
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
//获取 controller对象
AccountController bean = context.getBean(AccountController.class);
bean.save(new Account(1,"张三",10.2));
}
}
测试3:
public class Test3 {
/**
完善 JdbcConfig 类的功能
1:使用注解形式完成外部属性文件(jdbc.properties)的引入。参考注解:@PropertySource
2: 将引入的文件中的属性注入到 成员变量上。参考注解: @Value注解
3: 将获取数据库连接池方法返回值,当做bean教给Spring管理.参考注解@Bean。
4: SpringConfig配置类 整合 JdbcConfig配置类。参考注解@Import。
5: 完成Test02测试,从Spring容器中获取连接池对象。
*/
@Test
public void Test03(){
// 创建 解析注解的容器类
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
DruidDataSource bean = context.getBean(DruidDataSource.class);
System.out.println(bean);
}
}
测试4:
// 代表我们要使用spring重写的测试运行器
@RunWith(SpringJUnit4ClassRunner.class)
//在类上面加注解 读取配置文件(配置类) 在做测试的时候 测试对象中自动完成容器的创建
@ContextConfiguration(classes = SpringConfig.class)
public class Test4 {
@Autowired
private AccountController accountController;
@Autowired
private DruidDataSource druidDataSource;
@Test
public void test04(){
accountController.save(new Account(2,"张斯",1.1));
System.out.println(druidDataSource);
}
}