Spring纯注解开发,只是将xml配置项移到注解配置,我们只需要将xml和注解一一对应迁移即可
常用注解说明
| 注解 | 对应XML标签 | 说明 |
|---|---|---|
表示此类交给SpringIOC容器管理注解 | ||
| @Component | <bean/> | 表示此类交给SpringIOC容器管理 |
| @Controller | <bean/> | 组合注解(组合了@Component注解),应用在MVC层(控制层),DispatcherServlet会自动扫描注解了此注解的类,然后将web请求映射到注解了@RequestMapping的方法上。 |
| @Service | <bean/> | 组合注解(组合了@Component注解),应用在service层(业务逻辑层) |
| @Reponsitory | <bean/> | 组合注解(组合了@Component注解),应用在dao层(数据访问层) |
| @Scope | <bean scope=""/> | 配合以上四个注解,指定被管理类的生命周期singleton,prototype等 |
表示从IOC容器中获取Bean注解 | ||
| @Autowired | <property/> | Spring提供的工具(由Spring的依赖注入工具(BeanPostProcessor、BeanFactoryPostProcessor)根据类型自动注入。) |
| @Qualifier | / | @Autowired的辅助注解,帮助确认唯一Bean |
| @Resource | <property/> | JSR-250提供的注解,注入属性使用,JDK11删除不建议使用 |
以下是迁移配置文件,在配置类中使用的注解 | ||
| @Configuration | / | 声明当前类是一个配置类(相当于一个Spring配置的xml文件) |
| @ComponentScan | <context:component-scan/> | 自动扫描指定包下所有使用@Service,@Component,@Controller,@Repository的类并注册 |
| @PropertySource | <context:property-placeholder/> | 指定引入外部属性资源文件 |
| @Value | / | 指定从外部资源文件获取数据@Value("${jdbc.url}") |
| @Import | <import/> | 指定引入其他配置类 |
| @Bean | <bean/> | 注解在方法上,声明当前方法的返回值为一个Bean。返回的Bean对应的类中可以定义init()方法和destroy()方法,然后在@Bean(initMethod=“init”,destroyMethod=“destroy”)定义,在构造之后执行init,在销毁之前执行destroy。 |
配置
项目内类交给IOC容器管理和获取
// 指定数据访问层,交给IOC容器管理
@Repository("accountDao")
public class JdbcAccountDaoImpl implements AccountDao {
// 根据类型注入实例
@Autowired
private ConnectionUtils connectionUtils;
}
// 指定业务Service层交给IOC容器管理
@Service("transferService")
public class TransferServiceImpl implements TransferService {
// 根据类型注入实例
@Autowired
// 对于注入类型有多实现的,使用此注解确定唯一,参数为id
@@Qualifier("accountDao")
private AccountDao accountDao;
}
// 指定业务Controller层交给IOC容器管理
@Controller("testController")
public class TestController {
// 根据类型注入实例
@Autowired
private TransferService transferService;
}
// 指定其他类,交给IOC容器管理
@Component("transactionManager")
public class TransactionManager {
......
}
配置文件迁移
// 指定类为配置类
@Configuration
// 指定扫描包
@ComponentScan({"com.wjy"})
// 引入外部资源文件
@PropertySource({"classpath:jdbc.properties"})
// @Import({OtherConfig.class}) 引入其他配置类
public class SpringConfig {
// 读取外部资源文件数据
@Value("${jdbc.driver}")
private String driver;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
// 表示方法返回值交给IOC容器管理,指定id
@Bean("dataSource")
public DataSource getDataSource() {
final DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}
}
启动方式
JavaSE项目
final ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
final AccountDao accountDao = (AccountDao) context.getBean("accountDao");
System.out.println(accountDao);
JavaWeb项目
修改WEB-INF/web.xml
<!--告诉ContextLoaderListener,我们使用注解启动-->
<context-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>
<!--指定spring配置类路径-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.wjy.SpringConfig</param-value>
</context-param>
<!--使用spring监听器启动IOC容器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
版权声明:本文为u010689440原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。