Spring基于Java注解的配置
2017-01-23 20:33
393 查看
前面已经讲了自动扫包配置和基于XML的配置,这一节讲基于Java的配置
(1)entity
厨师类,增加了唯一id
SpringBeans类:主要用来创建各种bean:
@Configuration 代表这是一个配置文件,
@Bean跟我们之前用的 XML中的bean标签是一致的。
基于java的配置有一个好处,就是在注入属性值的时候更加直观,不再是用property/>标签
(2)配置文件:只有一个自动扫包。
(3)测试类;
这里我们稍许改动,因为我们只是注册了一个bean,但是我们两次取出bean的id,目的是为了测试使用java配置的时候,产生的bean是否也是单例,因为在配置文件使用了new,这里会产生一些歧义。
(4)运行结果:
两次输出的id是一样的,说明基于Java的装配bean默认也是单例的。
完整代码的github地址。
(1)entity
厨师类,增加了唯一id
package spring.ch2.topic8; import org.springframework.beans.factory.annotation.Value; /** * Created by louyuting on 17/1/23. */ public class Chief { private final int id = index++; private static int index = 0; public int getId() { return id; } @Value("jack") private String name = ""; public String getName() { return name; } public void setName(String name) { this.name = name; } }
SpringBeans类:主要用来创建各种bean:
package spring.ch2.topic8; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * Created by louyuting on 17/1/23. */ @Configuration public class SpringBeans { @Bean public Chief jack() { Chief chief = new Chief(); chief.setName("mike"); return chief; } }
@Configuration 代表这是一个配置文件,
@Bean跟我们之前用的 XML中的bean标签是一致的。
基于java的配置有一个好处,就是在注入属性值的时候更加直观,不再是用property/>标签
(2)配置文件:只有一个自动扫包。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <context:component-scan base-package="spring.ch2.topic8" /> </beans>
(3)测试类;
package spring.ch2.topic8; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * Created by louyuting on 17/1/20. */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"/spring/ch2/topic8/ApplicationContext-test.xml"}) public class ChiefTest { @Autowired private ApplicationContext applicationContext; @Test public void testChief(){ Chief jack1 = (Chief)applicationContext.getBean(Chief.class); System.out.println(jack1.getId()); Chief jack = (Chief)applicationContext.getBean("jack"); System.out.println(jack.getId()); } }
这里我们稍许改动,因为我们只是注册了一个bean,但是我们两次取出bean的id,目的是为了测试使用java配置的时候,产生的bean是否也是单例,因为在配置文件使用了new,这里会产生一些歧义。
(4)运行结果:
0 0
两次输出的id是一样的,说明基于Java的装配bean默认也是单例的。
完整代码的github地址。
相关文章推荐
- Spring基于java注解配置
- Spring学习(13)--- 基于Java类的配置Bean 之 @Configuration & @Bean注解
- Spring学习(14)--- 基于Java类的配置Bean 之 @ImportResource & @Value 注解
- java Spring 基于注解的配置(一)
- spring基于java注解的配置
- Spring3.X学习笔记3-基于注解和基于Java类的配置
- Spring - 基于Java配置的注解
- Spring 基于XML配置 基于注解配置 基于JAVA类配置比较以及适用场景
- Spring学习(15)--- 基于Java类的配置Bean 之 @Bean & @Scope 注解
- 【Java.Spring.Core】【IoC】基于注解的Bean装配(非XML配置)
- 从头认识Spring-2.8 基于java注解的配置(基本没有xml)
- java调用基于https的webservice(不生成密钥,基于spring配置,接收所有证书)
- spring 基于注解的控制器配置
- spring aop的使用(注解方式以及基于xml配置方式)
- Java 学习笔记06:Spring 基于注解(Annotation)的AOP
- Spring 3.0.5 MVC 基于注解ehcache.xml 配置方式
- Spring 中 MongDB java Bean 的映射配置(注解方式)
- Spring基于注解的缓存配置--EHCache AND OSCache
- Java 学习笔记05:Spring 基于注解(Annotation)的IOC
- Java程序员从笨鸟到菜鸟之(七十二)细谈Spring(四)利用注解实现spring基本配置详解