Spring—注解开发
Spring原始注解
Spring是轻代码而重配置的框架,配置比较繁重,影响开发效率,所以注解开发是一种趋势,注解代替xml配置文 件可以简化配置,提高开发效率。
- @Component 使用在类上用于实例化Bean
- @Controller 使用在web层类上用于实例化Bean
- @Service 使用在service层类上用于实例化Bean
- @Repository 使用在dao层类上用于实例化Bean
- @Autowired 使用在字段上用于根据类型依赖注入
- @Qualifier 结合@Autowired一起使用用于根据名称进行依赖注入
- @Resource 相当于@Autowired+@Qualifier,按照名称进行注入
- @Value 注入普通属性
- @Scope 标注Bean的作用范围
- @PostConstruct 使用在方法上标注该方法是Bean的初始化方法
- @PreDestroy 使用在方法上标注该方法是Bean的销毁方法
注意:
使用注解进行开发时,需要在applicationContext.xml中配置组件扫描,作用是指定哪个包及其子包下的Bean需要 进行扫描以便识别使用注解配置的类、字段和方法。
<?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"xmlns:p="http://www.springframework.org/schema/p"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
"><context:component-scan base-package="com"/>
<context:property-placeholder location="jdbc.properties"/>
</beans>
使用@Compont或@Repository标识UserDaoImpl需要Spring进行实例化。
@Service("service")
@Scope("singleton")
public class service {@Autowired@Qualifier("impl")Dao dao;@Value("12")int no;@Value("${jdbc.username}")String name;public service(Dao dao) {this.dao = dao;}public service() {}public static void main(String[] args) {ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");service i = (service) applicationContext.getBean("service");i.save();}public void save() {System.out.println(name);dao.save();}public void setDao(Dao dao) {this.dao = dao;}
}
@Repository("impl")
@Scope("singleton")
public class ImplDao implements Dao {public void save() {System.out.println("saving");}public static void main(String[] args) {ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");ImplDao i = (ImplDao) applicationContext.getBean("impl");i.save();}
}
Spring新注解
使用上面的注解还不能全部替代xml配置文件,还需要使用注解替代的配置如下:
非自定义的Bean的配置:
加载properties文件的配置:context:property-placeholder
组件扫描的配置:context:component-scan
引入其他文件:
@Configuration 用于指定当前类是一个 Spring 配置类,当创建容器时会从该类上加载注解
@ComponentScan 用于指定 Spring 在初始化容器时要扫描的包。 作用和在 Spring 的 xml 配置文件中 的 <context:component-scan base-package=“com.itheima”/>一样
@Bean 使用在方法上,标注将该方法的返回值存储到 Spring 容器中
@PropertySource 用于加载.properties 文件中的配置
@Import 用于导入其他配置类
@Configuration
@ComponentScan("com")
@Import(DataSourceConfiguration.class)
public class SpringConfiguration {@Testpublic void test(){ApplicationContext applicationContext=new AnnotationConfigApplicationContext(SpringConfiguration.class);service service=(service) applicationContext.getBean("service");service.save();javax.sql.DataSource dataSource=(javax.sql.DataSource)applicationContext.getBean("datasource");try {System.out.println(dataSource.getConnection());} catch (SQLException throwables) {throwables.printStackTrace();}}
}
@PropertySource("classpath:jdbc.properties")
public class DataSourceConfiguration {@Value("${jdbc.driver}")String clazz;@Value("${jdbc.url}")String url;@Value("${jdbc.username}")String user;@Value("${jdbc.password}")String password;@Bean("datasource")public ComboPooledDataSource testC3P0() throws Exception {ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();comboPooledDataSource.setDriverClass(clazz);comboPooledDataSource.setJdbcUrl(url);comboPooledDataSource.setUser(user);comboPooledDataSource.setPassword(password);return comboPooledDataSource;}
}
Spring—注解开发相关推荐
- spring注解开发:容器中注册组件方式
1.包扫描+组件标注注解 使用到的注解如下,主要针对自己写的类 @Controller @Service @Repository @Component @ComponentScan 参考 spring ...
- 关于Spring注解开发教程,打包全送你
摘要:spring是我们web开发中必不可少的一个框架,基于传统的xml方式配置bean总觉得太过繁琐,从spring2.5之后注解的出现可以大大简化我们的配置. 本文分享自华为云社区<如何高效 ...
- spring注解开发配置spring父子容器
spring注解开发配置spring父子容器 官网 https://docs.spring.io/spring-framework/docs/current/spring-framework-refe ...
- Spring注解开发-Bean注册
1.使用注解开发我们需要在applicationContext.xml文件中添加context标签. 在配置文件中开启注解扫描. <?xml version="1.0" en ...
- Spring注解开发入门教程
注解开发: 什么是驱动注解? 注解启动时使用注解的形式替代xml配置,将繁杂的spring配置文件从工程中彻底消除掉,简化书写 注解驱动的弊端 为了达成注解驱动的目的,可能会将原先很简单的书写,变的更 ...
- 第二章 ---- spring注解开发
文章目录 参考视频 注解驱动的意义 常用注解(重点) 启动注解驱动 IoC bean定义(@Component .@Controller.@Service. @Repository) @Scope b ...
- ❤️使用Spring注解开发(建议收藏)
❤️使用注解开发 在Spring4之后,要使用注解开发,必须保证aop的包导入了 使用注解开发需要导入context约束,增加注解的支持! <?xml version="1.0&quo ...
- Spring注解开发学习笔记
1 IOC 1.1 工厂模式 使用工厂中方法代替new形式创建对象的一种设计模式 1.2 Inversion of Control控制翻转 一种思想,用于消减代码间的耦合. 实现思想:利用工厂设计模式 ...
- 二、Java框架之Spring注解开发
文章目录 1. IOC/DI注解开发 1.1 Component注解 @Component @Controller @Service @Repository 1.2 纯注解开发模式 1.3 注解开发b ...
最新文章
- CVPR2020论文点评: AdderNet(加法网络)
- 计算机硬盘怎么增加e盘和f盘,如何在我的电脑里新建一个磁盘区
- UIScrollView控件常用属性
- 在云服务器上执行C程序和python程序(centos系统)
- Google Drive客户端
- mysql select in 不存在返回0_MySQL索引优化看这篇文章就够了!
- linux 修I改资源限制1024,Re:如何解决1024的限制???
- FCKEditor报java.lang.NullPointerException
- 如何编写兼容各主流邮箱的HTML邮件
- 一个奇怪的DNS服务器故障
- iOS开发UIScrollView常见属性和方法
- “仁、义、礼、智、信、恕、忠、孝、悌、、节、恕、勇、让”
- Android MMKV框架引入使用
- java过滤空号了停机号_手机空号、停机、注销,空号检测为你去除无效号码
- 安卓平板python编程软件下载_Notepad++中文版
- 用html写QQ邮箱注册页面,制作简易QQ邮箱登录页面
- 转: 诺贝尔奖得主:东亚教育浪费了太多生命
- python re search match_简诉Python Re模块中re.search和re.match的区别
- 别只关注地段、户型 楼盘隐形品质不能忽视
- zabbix服务端搭建
热门文章
- select、poll、epoll使用小结
- fork创建多个子进程
- 1042. 字符统计(20)
- 【C++ Primer | 08】课后习题答案
- Java面试题整理,java常用排序算法图解
- 80后程序员月薪30K+感慨中年危机,面试必问!
- JavaScript知识笔记(三)——内置对象、浏览器对象
- JAVA的值传递问题
- JAVA基础——时间Date类型转换
- 最感叹的莫过于一见如故,最悲伤的莫过于再见陌路。最深的孤独,是你明知道自己的渴望,却得对它装聋作哑。最美的你不是生如夏花,而是在时间的长河里,波澜不惊。...