Java-based configuration @configuration and @bean usage in spring (RPM)

Source: Internet
Author: User

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?
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="Http://www.springframework.org/schema/beans"
  3. xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns:aop= "http://www.springframework.org/schema/ AOP "
  4. xmlns:tx= "http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
  5. xmlns:context="Http://www.springframework.org/schema/context"
  6. Xsi:schemalocation= "
  7. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  8. http://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
  9. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
  10. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-3.2.xsd ">
  11. <context:component-scan base-package="Springstudy.model" >
  12. </context:component-scan>
  13. </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?
  1. Package springstudy;
  2. Import Org.springframework.context.annotation.Bean;
  3. Import org.springframework.context.annotation.Configuration;
  4. Import SpringStudy.Model.Counter;
  5. Import SpringStudy.Model.Piano;
  6. @Configuration
  7. Public class Springconfig {
  8. @Bean
  9. Public Piano Piano () {
  10. return new Piano ();
  11. }
  12. @Bean (name = "Counter")
  13. Public Counter Counter () {
  14. return new Counter ("Shake It Off", piano ());
  15. }
  16. }

Third, the basic class code

Counter:

[Java]View PlainCopy print?
  1. Package Springstudy.model;
  2. Public class Counter {
  3. Public Counter () {
  4. }
  5. Public Counter (double multiplier, String song,instrument instrument) {
  6. this.multiplier = multiplier;
  7. this.song = song;
  8. this.instrument=instrument;
  9. }
  10. private Double multiplier;
  11. private String Song;
  12. @Resource
  13. private instrument instrument;
  14. public Double Getmultiplier () {
  15. return multiplier;
  16. }
  17. public void Setmultiplier (double multiplier) {
  18. this.multiplier = multiplier;
  19. }
  20. Public String Getsong () {
  21. return song;
  22. }
  23. public void Setsong (String song) {
  24. this.song = song;
  25. }
  26. Public Instrument getinstrument () {
  27. return instrument;
  28. }
  29. public Void Setinstrument (instrument instrument) {
  30. this.instrument = instrument;
  31. }
  32. }

Piano class

[Java]View PlainCopy print?
  1. Package Springstudy.model;
  2. Public class Piano {
  3. private String name="Piano";
  4. Private String sound;
  5. Public String GetName () {
  6. return name;
  7. }
  8. public void SetName (String name) {
  9. this.name = name;
  10. }
  11. Public String Getsound () {
  12. return sound;
  13. }
  14. public void Setsound (String sound) {
  15. this.sound = sound;
  16. }
  17. }


Iv. calling the test class

[Java]View PlainCopyprint?
  1. Package webmybatis;
  2. Import Org.springframework.context.ApplicationContext;
  3. Import Org.springframework.context.annotation.AnnotationConfigApplicationContext;
  4. Import SpringStudy.Model.Counter;
  5. Public class Springtest {
  6. public static void Main (string[] args) {
  7. //applicationcontext ctx = new Classpathxmlapplicationcontext ("Spring/bean.xml");//Read the contents of Bean.xml
  8. ApplicationContext annotationcontext = new Annotationconfigapplicationcontext ("Springstudy");
  9. Counter C = Annotationcontext.getbean ("Counter", Counter. Class); //Create a Reference object for the bean
  10. System.out.println (C.getmultiplier ());
  11. System.out.println (C.isequals ());
  12. System.out.println (C.getsong ());
  13. System.out.println (C.getinstrument (). GetName ());
  14. }
  15. }

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.