养成阅读好习惯,从关注开始!
xml配置spring
1.1 用于创建对象的注解
此类注解的作用就和在XML配置文件中编写一个
相当于xml配置中的
@Component 用于把当前类对象存入spring容器中
属性:value:用于指定bean的id。当我们不写时,它的默认值是当前类名,且首字母小写。
@Controller 一般用在表现层
@Service 一般用在业务层
@Repository 一般用在持久层
以上三个注解他们的作用和属性与Component是一模一样,可以不按照规定写注解。他们三个是spring框架为我们提供明确的三层使用的注解,使我们的三层对象更加清晰。
1.2 用于注入数据的注解
此类注解的作用就和在xml配置文件中的bean标签中写一个
相当于xml配置中的
@Autowired 自动按照类型注入。
注解位置:可以是变量上,也可以是方法上。
@Qualifier 在按照类中注入的基础之上再按照名称注入,需要和@Autowired注解配合使用。它在给类成员注入时不能单独使用。但是在给方法参数注入时可以。
属性:value:用于指定注入bean的id。
@Resource 直接按照bean的id注入。它可以独立使用,通过名称Bean名称进行依赖注入。 属性:name:用于指定bean的id。
@Value 用于注入基本类型和String类型的数据
属性:value:用于指定数据的值。它可以使用spring中SpEL(也就是spring的el表达式)
SpEL的写法:${表达式}
1.3 用于修改作用范围的注解
此注解的作用就和在bean标签中使用scope属性实现的功能是一样的
相当于xml配置中的
@Scope
作用:用于指定bean的作用范围
属性:value:指定范围的取值。常用取值:singleton 单例的 prototype 多例的
1.4 与声明周期相关的注解
此类注解的作用就和在bean标签中使用init-method和destroy-methode的作用是一样的
相当于xml配置中的
@PostConstruct 用于指定初始化方法
@PreDestroy 用于指定销毁方法
代码示例:
@Service
//@Scope("prototype")
public class AccountServiceImpl implements AccountService {
// @Autowired
// @Qualifier("accountDao1")
@Resource(name = "accountDao2")
private AccountDao accountDao;
@Value(value = "Bruce")
private String name;
@Value("30")
private Integer age;
@Autowired
private Date birthday;
@PostConstruct
public void init() {
System.out.println("初始化方法执行。。。。");
}
@PreDestroy
public void destory() {
System.out.println("销毁方法执行。。。。");
}
public void saveAccount() {
System.out.println("Service创建的账户保存了");
accountDao.saveAccount();
}
public void show() {
System.out.println(name + " " + age + " " + birthday);
}
}
1.5 spring中xml配置和注解之间的关系
注解配置优势:配置简单,易于维护(配置都在对应的类上)
xml配置的优势:修改时不需要重新编译,通过外部引入
spring中Bean管理方式的对比
通过注解获取IoC容器:
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class);
书写在src下的resources中
配置代码书写在config包中与项目包平级
SpringConfiguration类
@ComponentScan("cn.bruce")
@Import(JdbcConfig.class)
@PropertySource("classpath:jdbcConfig.properties")
public class SpringConfiguration {
}
JdbcConfig类
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;
/**
* 创建数据源对象
* @return
*/
@Bean(name = "ds")
public DataSource creatDataSource(){
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);
}
}
/**
* 创建QueryRunner对象
* @param dataSource
* @return
*/
@Bean(name = "runner")
@Scope("prototype")
public QueryRunner creatQueryRunner(@Qualifier("ds") DataSource dataSource){
return new QueryRunner(dataSource);
}
}
Junit执行问题:
Spring整合Junit:
代码示例
maven坐标:
org.springframework
spring-test
5.2.8.RELEASE
test测试类:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfiguration.class)
public class AccountServiceTest {
@Autowired
private AccountService accountService = null;
@Test
public void testFindAll(){
List accounts = accountService.findAll();
for(Account account : accounts){
System.out.println(account);
}
}
}
注意:
① 当我们使用spring 5.x版本的时候,要求junit的jar必须是4.12及以上
② 当通过xml配置spring时,本地路径注意不要有空格@ContextConfiguration(locations = "classpath:beans.xml")
最后说一句,如果本文对你有帮助,请三连支持一下哦!