你的位置:首页 > Java教程

[Java教程]CREATING SPRING BEANS


There are several ways of defining Spring Beans in the Spring Framework. Each method has its own advantages and disadvantages. You may be wondering which one should you use. The answer is, it depends. The reality is in a large enterprise Spring project, you will likely encounter a combination of methods in defining Spring Beans. Neither is wrong, nor necessarily better. In legacy Spring applications, you’re likely to encounter

In this post, I’ll step you through common ways of defining Spring Beans. At the end of the post, I’ll show you how the different methods work seamlessly.

Say Hello Spring Bean

All the examples in this post will implement the following Interface. We will use simple Hello World type beans which will print a message to the system console.

1 package guru.springframework.hello;2 3 /**4  * Created by jt on 3/28/15.5 */6 public interface HelloBean {7   void sayHello();8 }

HelloBean

Spring Configuration

Hello Spring Bean

Here is the Spring Bean we will use for this example.

 1 package guru.springframework. 2  3 import guru.springframework.hello.HelloBean; 4  5 /** 6  * Created by jt on 3/28/15. 7 */ 8 public class implements HelloBean { 9   @Override10   public void sayHello() {11     System.out.println("Hello, I'm a );12   }13 }


Spring
1 <?2 <beans 3    4    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">5 6    <bean id="class="guru.springframework.7 </beans>

Spring

Application

The following code can be used to execute the example Spring Bean.

 1 package guru.springframework. 2  3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.context.ApplicationContext; 6 import org.springframework.context.annotation.ImportResource; 7  8 /** 9  * Created by jt on 3/28/15.10 */11 @SpringBootApplication12 @ImportResource("classpath*:spring/spring-config.)13 public class 14   public static void main(String[] args) {15     ApplicationContext ctx = SpringApplication.run(class, args);16     );17     bean.sayHello();18   }19 }


Output

 1  .  ____     _      __ _ _ 2 /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ 3 ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ 4 \\/ ___)| |_)| | | | | || (_| | ) ) ) ) 5  ' |____| .__|_| |_|_| |_\__, | / / / / 6 =========|_|==============|___/=/_/_/_/ 7 :: Spring Boot ::    (v1.2.2.RELEASE) 8  9 2015-03-28 10:38:02.260 INFO 66700 --- [      main] guru.springframework.springbeans)10 2015-03-28 10:38:02.319 INFO 66700 --- [      main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@3e57cd70: startup date [Sat Mar 28 10:38:02 EDT 2015]; root of context hierarchy11 2015-03-28 10:38:03.229 INFO 66700 --- [      main] o.s.b.f.config.12 2015-03-28 10:38:03.656 INFO 66700 --- [      main] o.s.j.e.a.AnnotationMBeanExporter    : Registering beans for JMX exposure on startup13 2015-03-28 10:38:03.669 INFO 66700 --- [      main] guru.springframework.for 2.311)14 Hello, I'm a 15 2015-03-28 10:38:03.670 INFO 66700 --- [    Thread-1] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@3e57cd70: startup date [Sat Mar 28 10:38:02 EDT 2015]; root of context hierarchy16 2015-03-28 10:38:03.671 INFO 66700 --- [    Thread-1] o.s.j.e.a.AnnotationMBeanExporter    : Unregistering JMX-exposed beans on shutdown

Output

Java Based Configuration

In Spring 3, Java based configuration was released. The Spring community is gradually converting over to prefer the use of Java based configuration over

Hello Spring Bean

 1 package guru.springframework.javaconfig; 2  3 import guru.springframework.hello.HelloBean; 4  5 /** 6  * Created by jt on 3/28/15. 7 */ 8 public class JavaConfigBean implements HelloBean { 9   @Override10   public void sayHello() {11     System.out.println("Hello, I'm a Java Config bean");12   }13 }

JavaConfigBean

Java Configuration Class

The following class is a Spring Configuration class. The annotation @Configuration  at the top of the class defines the class as a configuration class. The class will be picked up in a Spring component scan. It can also be imported into other configuration classes too. Objects returned  @Bean  annotation will be registered as Spring Beans. By convention the name of the registered Spring Bean will be set equal to the name of the method. 

 1 package guru.springframework.javaconfig; 2  3 import org.springframework.context.annotation.Bean; 4 import org.springframework.context.annotation.Configuration; 5  6 /** 7  * Created by jt on 3/28/15. 8 */ 9 @Configuration10 public class JavaConfig {11   @Bean12   public JavaConfigBean javaConfigBean(){13     return new JavaConfigBean();14   }15 }

JavaConfig

Application

The following code can be used to execute the example Spring Bean.

 1 package guru.springframework.javaconfig; 2  3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.context.ApplicationContext; 6 import org.springframework.context.annotation.Import; 7  8 /** 9  * Created by jt on 3/28/15.10 */11 @SpringBootApplication12 @Import(JavaConfig.class)13 public class JavaConfigApplication {14   public static void main(String[] args) {15     ApplicationContext ctx = SpringApplication.run(JavaConfigApplication.class, args);16     JavaConfigBean bean = (JavaConfigBean) ctx.getBean("javaConfigBean");17     bean.sayHello();18   }19 }

JavaConfigApplication

Output

 1  .  ____     _      __ _ _ 2 /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ 3 ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ 4 \\/ ___)| |_)| | | | | || (_| | ) ) ) ) 5  ' |____| .__|_| |_|_| |_\__, | / / / / 6 =========|_|==============|___/=/_/_/_/ 7 :: Spring Boot ::    (v1.2.2.RELEASE) 8  9 2015-03-28 11:13:46.387 INFO 66847 --- [      main] g.s.javaconfig.JavaConfigApplication   : Starting JavaConfigApplication on Johns-MacBook-Pro.local with PID 66847 (/Users/jt/src/springframework.guru/blog/springbeans/target/classes started by jt in /Users/jt/src/springframework.guru/blog/springbeans)10 2015-03-28 11:13:46.447 INFO 66847 --- [      main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@3e57cd70: startup date [Sat Mar 28 11:13:46 EDT 2015]; root of context hierarchy11 2015-03-28 11:13:47.579 INFO 66847 --- [      main] o.s.j.e.a.AnnotationMBeanExporter    : Registering beans for JMX exposure on startup12 2015-03-28 11:13:47.607 INFO 66847 --- [      main] g.s.javaconfig.JavaConfigApplication   : Started JavaConfigApplication in 1.651 seconds (JVM running for 2.265)13 Hello, I'm a Java Config bean14 2015-03-28 11:13:47.609 INFO 66847 --- [    Thread-1] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@3e57cd70: startup date [Sat Mar 28 11:13:46 EDT 2015]; root of context hierarchy15 2015-03-28 11:13:47.610 INFO 66847 --- [    Thread-1] o.s.j.e.a.AnnotationMBeanExporter    : Unregistering JMX-exposed beans on shutdown

Output

Annotation Based Configuration

Spring also can detect beans based on class level annotations. Any class annotated with with @Component ,@Service , @Controller , or @Repository  will be considered a Spring Bean. The class must reside in a package which had been identified for a component scan. Spring will scan the package for annotated classes. Any class detected will be managed by the Spring Context at run time.

Hello Spring Bean

 1 package guru.springframework.componentscan; 2  3 import guru.springframework.hello.HelloBean; 4 import org.springframework.stereotype.Component; 5  6 /** 7  * Created by jt on 3/28/15. 8 */ 9 @Component10 public class ComponentScanBean implements HelloBean {11   public void sayHello(){12     System.out.println("Hello, I'm a Component Scan Bean");13   }14 }

ComponentScanBean

Application

The following code can be used to execute the example Spring Bean.

 1 package guru.springframework.componentscan; 2  3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.context.ApplicationContext; 6  7 /** 8  * Created by jt on 3/28/15. 9 */10 @SpringBootApplication11 public class ComponentScanApplication {12   public static void main(String[] args) {13     ApplicationContext ctx = SpringApplication.run(ComponentScanApplication.class, args);14     ComponentScanBean bean = (ComponentScanBean) ctx.getBean("componentScanBean");15     bean.sayHello();16   }17 }

ComponentScanApplication

Output

 1  .  ____     _      __ _ _ 2 /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ 3 ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ 4 \\/ ___)| |_)| | | | | || (_| | ) ) ) ) 5  ' |____| .__|_| |_|_| |_\__, | / / / / 6 =========|_|==============|___/=/_/_/_/ 7 :: Spring Boot ::    (v1.2.2.RELEASE) 8  9 2015-03-28 15:21:17.576 INFO 67246 --- [      main] g.s.c.ComponentScanApplication      : Starting ComponentScanApplication on Johns-MacBook-Pro.local with PID 67246 (/Users/jt/src/springframework.guru/blog/springbeans/target/classes started by jt in /Users/jt/src/springframework.guru/blog/springbeans)10 2015-03-28 15:21:17.632 INFO 67246 --- [      main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@3e57cd70: startup date [Sat Mar 28 15:21:17 EDT 2015]; root of context hierarchy11 2015-03-28 15:21:18.850 INFO 67246 --- [      main] o.s.j.e.a.AnnotationMBeanExporter    : Registering beans for JMX exposure on startup12 2015-03-28 15:21:18.867 INFO 67246 --- [      main] g.s.c.ComponentScanApplication      : Started ComponentScanApplication in 1.671 seconds (JVM running for 2.218)13 Hello, I'm a Component Scan Bean14 2015-03-28 15:21:18.869 INFO 67246 --- [    Thread-1] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@3e57cd70: startup date [Sat Mar 28 15:21:17 EDT 2015]; root of context hierarchy15 2015-03-28 15:21:18.870 INFO 67246 --- [    Thread-1] o.s.j.e.a.AnnotationMBeanExporter    : Unregistering JMX-exposed beans on shutdown

Output

Using Spring Bean Configurations Together

While there are different ways of defining Spring Beans, its important to remember the different methods can be used together. Its very common to use multiple methods of defining Spring Beans in a single application.

Spring Bean Application

We can use the beans previously defined to demonstrate how this works seamlessly. The Spring component scan functionality will detect classes annotated as Spring Beans, and classes marked as configuration classes which also define Spring Beans. In the following code example, the annotation@ComponentScan("guru.springframework") tells Spring to perform a component scan on the package guru.springframework.

This technique will work for annotated classes, but it not pick up @ImportResource("classpath*:spring/spring-config.) will bring the specified

The class below is annotated to perform the component scan, which will pick up Spring Beans defined in Java configuration classes, Spring Beans defined with annotations, and Spring Beans defined in

 1 package springbeans; 2  3 import guru.springframework.componentscan.ComponentScanBean; 4 import guru.springframework.hello.HelloBean; 5 import guru.springframework.javaconfig.JavaConfigBean; 6 import guru.springframework. 7 import org.springframework.boot.SpringApplication; 8 import org.springframework.boot.autoconfigure.SpringBootApplication; 9 import org.springframework.context.ApplicationContext;10 import org.springframework.context.annotation.ComponentScan;11 import org.springframework.context.annotation.ImportResource;12 13 import java.util.ArrayList;14 import java.util.List;15 16 @SpringBootApplication17 @ComponentScan("guru.springframework")18 @ImportResource("classpath*:spring/spring-config.)19 public class SpringBeansApplication {20 21   public static void main(String[] args) {22     ApplicationContext ctx = SpringApplication.run(SpringBeansApplication.class, args);23 24     List<HelloBean> helloBeans = new ArrayList<HelloBean>();25     helloBeans.add((ComponentScanBean) ctx.getBean("componentScanBean"));26     helloBeans.add((JavaConfigBean) ctx.getBean("javaConfigBean"));27     helloBeans.add(());28 29     for(HelloBean helloBean : helloBeans){30       helloBean.sayHello();31     }32   }33 }

SpringBeansApplication

Output

When you run the above class, you will see the following output.

 1  .  ____     _      __ _ _ 2 /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ 3 ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ 4 \\/ ___)| |_)| | | | | || (_| | ) ) ) ) 5  ' |____| .__|_| |_|_| |_\__, | / / / / 6 =========|_|==============|___/=/_/_/_/ 7 :: Spring Boot ::    (v1.2.2.RELEASE) 8  9 2015-03-28 15:47:11.531 INFO 67265 --- [      main] springbeans.SpringBeansApplication    : Starting SpringBeansApplication on Johns-MacBook-Pro.local with PID 67265 (/Users/jt/src/springframework.guru/blog/springbeans/target/classes started by jt in /Users/jt/src/springframework.guru/blog/springbeans)10 2015-03-28 15:47:11.602 INFO 67265 --- [      main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@3e57cd70: startup date [Sat Mar 28 15:47:11 EDT 2015]; root of context hierarchy11 2015-03-28 15:47:12.544 INFO 67265 --- [      main] o.s.b.f.config.12 2015-03-28 15:47:12.725 INFO 67265 --- [      main] o.s.b.f.config.13 2015-03-28 15:47:12.753 INFO 67265 --- [      main] o.s.b.f.s.DefaultListableBeanFactory   : Overriding bean definition for bean 'class [guru.springframework.abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in URL [file:/Users/jt/src/springframework.guru/blog/springbeans/target/classes/spring/spring-config.class [guru.springframework.abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in URL [file:/Users/jt/src/springframework.guru/blog/springbeans/target/classes/spring/spring-config.14 2015-03-28 15:47:13.014 INFO 67265 --- [      main] o.s.j.e.a.AnnotationMBeanExporter    : Registering beans for JMX exposure on startup15 2015-03-28 15:47:13.028 INFO 67265 --- [      main] springbeans.SpringBeansApplication    : Started SpringBeansApplication in 1.878 seconds (JVM running for 2.474)16 Hello, I'm a Component Scan Bean17 Hello, I'm a Java Config bean18 Hello, I'm a 19 2015-03-28 15:47:13.029 INFO 67265 --- [    Thread-1] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@3e57cd70: startup date [Sat Mar 28 15:47:11 EDT 2015]; root of context hierarchy20 2015-03-28 15:47:13.030 INFO 67265 --- [    Thread-1] o.s.j.e.a.AnnotationMBeanExporter    : Unregistering JMX-exposed beans on shutdown

Output