spring注解

随着越来越多地使用Springboot敏捷开发,更多地使用注解配置Spring,而不是Spring的applicationContext.xml文件。

  • Configuration注解: Spring解析为配置类,相当于spring配置文件

  • Bean注解:容器注册Bean组件,默认id为方法名

    @Configuration public class AppConfig { @Bean public MyService myService() { return new MyServiceImpl(); } }

等同于beans.xml文件

1<beans> 2 <bean id="myService" class="com.acme.services.MyServiceImpl"/> 3</beans>

1)applicationContext.xml文件-包扫描

1@ComponentScans(value = {@ComponentScan(value = "com.self",excludeFilters = { 2 @Filter(type = FilterType.ANNOTATION,classes = {Controller.class}) 3 }) 4}) 5@Configuration 6public class RootConfig { 7 8 //测试Bean 9 @Bean 10 public Person person() { 11 return new Person("张励",22,"工程师"); 12 } 13}

2)导入properties文件

1@PropertySource(value = {"classpath:person.properties"}) 2@Configuration 3public class MainConfigOfProperty { 4 5 @Bean 6 public Person person() { 7 return new Person(); 8 } 9}

赋值

1public class Person { 2 3 @Value("${person.name}")//配置文件属性 4 private String name; 5 6}

3)数据源

1@EnableTransactionManagement//开启基于注解的事务管理功能 2@ComponentScan("com.self.ds") 3@Configuration 4public class TxConfig { 5 6 //数据源 7 @Bean 8 public DataSource dataSource() throws Exception{ 9 ComboPooledDataSource dataSource = new ComboPooledDataSource(); 10 dataSource.setUser("root"); 11 dataSource.setPassword("000111"); 12 dataSource.setDriverClass("com.mysql.jdbc.Driver"); 13 dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/self"); 14 return dataSource; 15 } 16 17 18 @Bean 19 public JdbcTemplate jdbcTemplate() throws Exception{ 20 JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource()); 21 return jdbcTemplate; 22 } 23 24 //事务管理器 25 @Bean 26 public PlatformTransactionManager transactionManager() throws Exception{ 27 return new DataSourceTransactionManager(dataSource()); 28 } 29 30 31}

单元测试

1public class IOCTest { 2 3 AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class); 4 5 @Test 6 public void test02() { 7 Object bean1 = applicationContext.getBean("person"); 8 Object bean2 = applicationContext.getBean("person"); 9 System.out.println( bean1 == bean2); 10 } 11 12 @Test 13 public void test01() { 14 Object bean = applicationContext.getBean("person01"); 15 System.out.println("结果: " + bean); 16 } 17 18 19 @Test 20 public void test() { 21 String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames(); 22 for(String beanDef:beanDefinitionNames) { 23 System.out.println("输出: " + beanDef); 24 } 25 26 } 27 28}

执行结果

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

spring源码解析

前言上篇我们介绍了spring容器加载的方式,并重点介绍了基于xml配置解析和注解扫描两种容器加载的方式,封装和注册beandefinition的过程。今天我们分享BeanDefinition注册后的另一个重要过程bean的实例化过程的源码。容器加载流程!spring源码解析spring容器加载源码(bean实

MyBatis接口(Bean)与配置信息(Mapper)绑定

目的MyBatis的XML配置文件解析成JAVA类并在内存中存储,但是在程序运行时需要对应的类去调用,而相应的调用类还没有实例化,现在流行的都是使用Spring去管理需要的对象,Spring提供2种方式,分别为XML与注解。下面来分析调用类的实例化及与配置绑定。1XML方式<bean id"menuMapper" cl

Spring 学习笔记(三):Spring Bean

1Bean配置Spring可以看做是一个管理Bean的工厂,开发者需要将Bean配置在XML或者Properties配置文件中。实际开发中常使用XML的格式,其中<bean中的属性或子元素如下:id:Bean在BeanFactory中的唯一标识,在代码中通过BeanFac