- Cloud community >
- Blog List >
- The body of the
Use Spring’s JavaConfig
Swim, swim, swim
Java Spring configures the String Annotation XML Class Schema bean
Abstract: Previously we defined beans in XML files, such as: 1 2 3 4 5 6 7 8
Previously we defined beans in XML files, such as:
< beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd "> < bean id =" helloBean" class="com.mkyong.hello.impl.HelloWorldImpl"> </ beans>Copy the code
We can use annotations to do this, such as the following code, which does the same thing as the XML configuration above:
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.mkyong.hello.HelloWorld; import com.mkyong.hello.impl.HelloWorldImpl; @Configuration public class AppConfig { @Bean(name= "helloBean") public HelloWorld helloWorld() { return new HelloWorldImpl(); }}Copy the code
Imagine a scenario where we have a large project and if all the beans were configured in an XML file, the file would be very large. So most of the time we split a large XML configuration file into several pieces. This will make it easier to manage. Finally, import into the overall XML file, such as:
< beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd "> < import resource =" XML config/customer. "/ > < import resource="config/scheduler.xml"/> </ beans>Copy the code
But now we can use JavaConfig to do the same thing:
import
org.springframework.context.annotation.Configuration;
import
org.springframework.context.annotation.Import;
@Configuration
@Import
({ CustomerConfig.class
, SchedulerConfig.class
})
public
class AppConfig {
}
Copy the code
Let’s take a look at a demo of this example:
CustomerBo.java
public class CustomerBo { public void printMsg(String msg) { System.out.println("CustomerBo : " + msg); }}Copy the code
SchedulerBo.java
public class SchedulerBo { public void printMsg(String msg) { System.out.println("SchedulerBo : " + msg); }}Copy the code
Now let’s use annotations:
@Configuration public class CustomerConfig { @Bean(name= "customer") public CustomerBo customerBo(){ return new CustomerBo(); }}Copy the code
@Configuration public class SchedulerConfig { @Bean(name= "scheduler") public SchedulerBo suchedulerBo(){ return new SchedulerBo(); }}Copy the code
AppConfig.java
import
org.springframework.context.annotation.Configuration;
import
org.springframework.context.annotation.Import;
@Configuration
@Import
({ CustomerConfig.class
, SchedulerConfig.class
})
public
class AppConfig {
}
Copy the code
Then run:
public class App { public static void main(String[] args) { ApplicationContext context = new AnnotationConfigApplicationContext( AppConfig.class ); CustomerBo customer = (CustomerBo) context.getBean("customer" ); customer.printMsg("Hello 1" ); SchedulerBo scheduler = (SchedulerBo) context.getBean("scheduler" ); scheduler.printMsg("Hello 2" ); }}Copy the code
Use the cloud habitat community APP, comfortable ~
For details, please click
Review articles (0)
- To:
-
- Previous article: Summary of spring 3’s approach to injecting values into beans
- Next: About the problem with urL-pattern using “/” to dispatcherservlets
Related articles
- JavaConfig features in Spring
- Spring In action (version 4) — 2.5 import and mixed configuration
- SpringBoot Debunked: Building microservices Quickly — Chapter 2…
- Spring In Action (4th edition) — 2.3 Through the Java generation…
- Detail the JavaConfig annotations for Java Spring
- Spring In Action (4th edition) — 2.4 Through XML assembly…
- Spring three bean assembly schemes
- Use Spring’s JavaConfig
- Spring Practice 2: Assemble beans – the essence of dependency injection
- “Spring Combat (4th edition)” — introduction