文章目录
一、使用注解和xml组合开发
创建并配置applicationContext.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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!--扫描指定的包,这个包下的注解就会生效-->
<context:component-scan base-package="com.sdpei"/>
<context:annotation-config />
</beans>
1.使用注解取代原本的中的实体类的注入
在实体类的上面添加@Component :作用相当于< bean id ="…" class="…"></ bean>
2.属性的注入
在非实体类的 “属性上” 或 “对应的set方法上” 面加上@Value("…")
3.引用类型属性的注入
在使用xml文件实现自动装配;使用注解@Autowired和@Resource实现自动装配【点击查看】中已经讲过这一块内容。
在引用类型的属性的上面加上@Autowired或@Resource
4.衍生注解
在javawe中,我们会遵循MVC三层架构,一般会存在dao\ pojo\ sercice\ controller层,不同层的类的注入方式是不同的,但是其作用都是相同的------交给spring容器托管。不需要再在XXX.xml文件中进行bean 的托管了。
在dao层我们使用@Repository: 其是在@Component上派生的,所有它有@Component的特性,实现将该类注入到spring容器中。也有自己独有的特点(如:类中抛出的数据访问异常封装为 Spring 的数据访问异常类型)
在pojo层我们使用@Component: 实现将该类注入到spring容器中。
在service层我们使用@Service: 其也是实现自动注入的,也是@Component的一个衍生注解,实现将该类注入到spring容器中。
在controller层我们使用@Controller: 其也是实现自动注入的,也是@Component的一个衍生注解,实现将该类注入到spring容器中。
5.自动装配
在使用xml文件实现自动装配;使用注解@Autowired和@Resource实现自动装配【点击查看】中已经讲过这一块内容。
@Autowired:通过byType进行装配.
@Autowired(required = false):表示被注解标记的对象可以为null.
@Autowired+@Qualilier(value="xxx "):value为bean的id.
@Resource:先通过byName进行装配,再通过byType进行装配.
@Resource(value=" xxx"):value为bean的id.
6.作用域
前面我们使用过< bean id="" class="" scope=“singleton/prototype”>实现的作用域
我们可以可直接在对象的类的上面使用@Scope(“singleton”)/@Scope(“prototype”)来实现作用域的设置
注意
在xml文件中一定要加上下面的配置
<context:component-scan base-package="com.sdpei"/>
<context:annotation-config />
上面的两个标签的目的是使com.sdper下的类中的相关的注解生效
测试
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserPojo userPojo = context.getBean("userPojo", UserPojo.class);
UserDao userDao = context.getBean("userDao", UserDao.class);
UserService userService = context.getBean("userService", UserService.class);
UserController userController = context.getBean("userController", UserController.class);
System.out.println(userPojo.toString());
userDao.getDao();
userService.getService();
userController.getController();
}
二、完全使用注解开发
从(一)中我们可以发现,我们还是使用到了xml文件。下面我们介绍一下如何摆脱xml文件。
1. 使用JavaConfig实现配置
在spring中,使用java取代xml文件的功能。我需要使用到两个注解@Configuration和@Bean
@Configuration
class public HJ(){
@Bean
public User getUser(){
return new User();
}
}
@Bean的作用相等于< bean id=“getUser” class="… …/User" />
例如:
2. 将多个“@Configuration类”融合在一起
我们知道,在spring中是可以有多个xml文件的,如果我们使用注解取代xml,怎么将多个@Configuration类融合在一起呢?
在一个@Configuration类中使用@import(类名.class)将多个@Configuration类进入一个@Configuration类中。
这样做的好处是,我们通过一个@Configuration类就可以获取到任意一个bean对象
例如:
测试
@Test
public void test2(){
ApplicationContext context = new AnnotationConfigApplicationContext(MainConfig.class);
UserDao userDao = context.getBean("getUserDao", UserDao.class);
UserPojo userPojo = context.getBean("getUserPojo", UserPojo.class);
UserController userController = context.getBean("getUserController", UserController.class);
UserService userService = context.getBean("getUserService", UserService.class);
userController.getController();
userDao.getDao();
System.out.println(userPojo.toString());
userService.getService();
}