In order to reduce the configuration in XML in spring, a configuration class (such as Springconfig) can be used to configure the bean.
First, a small amount of configuration in XML is required to start the Java configuration:
[Java]View PlainCopyprint?
- <?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:aop= "http://www.springframework.org/schema/ AOP "
- xmlns:tx= "http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
- xmlns:context="Http://www.springframework.org/schema/context"
- Xsi:schemalocation= "
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
- http://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-3.2.xsd ">
- <context:component-scan base-package="Springstudy.model" >
- </context:component-scan>
- </beans>
Second, define a configuration class
The class is annotated with @configuration, the equivalence is configured with XML beans, and the @bean annotation method is equivalent to the configuration bean in XML.
The code is as follows:
[Java]View PlainCopyprint?
- Package springstudy;
- Import Org.springframework.context.annotation.Bean;
- Import org.springframework.context.annotation.Configuration;
- Import SpringStudy.Model.Counter;
- Import SpringStudy.Model.Piano;
- @Configuration
- Public class Springconfig {
- @Bean
- Public Piano Piano () {
- return new Piano ();
- }
- @Bean (name = "Counter")
- Public Counter Counter () {
- return new Counter ("Shake It Off", piano ());
- }
- }
Third, the basic class code
Counter:
[Java]View PlainCopy print?
- Package Springstudy.model;
- Public class Counter {
- Public Counter () {
- }
- Public Counter (double multiplier, String song,instrument instrument) {
- this.multiplier = multiplier;
- this.song = song;
- this.instrument=instrument;
- }
- private Double multiplier;
- private String Song;
- @Resource
- private instrument instrument;
- public Double Getmultiplier () {
- return multiplier;
- }
- public void Setmultiplier (double multiplier) {
- this.multiplier = multiplier;
- }
- Public String Getsong () {
- return song;
- }
- public void Setsong (String song) {
- this.song = song;
- }
- Public Instrument getinstrument () {
- return instrument;
- }
- public Void Setinstrument (instrument instrument) {
- this.instrument = instrument;
- }
- }
Piano class
[Java]View PlainCopy print?
- Package Springstudy.model;
- Public class Piano {
- private String name="Piano";
- Private String sound;
- Public String GetName () {
- return name;
- }
- public void SetName (String name) {
- this.name = name;
- }
- Public String Getsound () {
- return sound;
- }
- public void Setsound (String sound) {
- this.sound = sound;
- }
- }
Iv. calling the test class
[Java]View PlainCopyprint?
- Package webmybatis;
- Import Org.springframework.context.ApplicationContext;
- Import Org.springframework.context.annotation.AnnotationConfigApplicationContext;
- Import SpringStudy.Model.Counter;
- Public class Springtest {
- public static void Main (string[] args) {
- //applicationcontext ctx = new Classpathxmlapplicationcontext ("Spring/bean.xml");//Read the contents of Bean.xml
- ApplicationContext annotationcontext = new Annotationconfigapplicationcontext ("Springstudy");
- Counter C = Annotationcontext.getbean ("Counter", Counter. Class); //Create a Reference object for the bean
- System.out.println (C.getmultiplier ());
- System.out.println (C.isequals ());
- System.out.println (C.getsong ());
- System.out.println (C.getinstrument (). GetName ());
- }
- }
NOTE: If you configure beans and beans in XML, or if you use an automatic scan call, the code is
ApplicationContext CTX = new Classpathxmlapplicationcontext ("Spring/bean.xml");//Read the contents of Bean.xml
Counter C = Ctx.getbean ("Counter", counter.class);//Create a Reference object for the bean
V. Results of operation
12.0
False
Shake it Off
Piano
This article turns from http://blog.csdn.net/vvhesj/article/details/47661001 thanks to the original author
Java-based configuration @configuration and @bean usage in spring (RPM)