Java Configuration
The Java configuration is the recommended configuration for spring 4.x, which can completely replace the XML configuration.
The Java configuration is also the Sping Boot recommended configuration method.
The Java configuration is implemented through @configuration and @bean.
@Configuration declares that the current class is a configuration class, which is equivalent to a spring-configured XML file.
@Bean annotations on a method, declares that the return value of the current method is a Bean.
Principle of Use:
Global configuration using Java configuration (such as database-related configuration, MVC-related configuration), business bean configuration using annotation configuration
(@Service, @Component, @Repository, @Controlle).
Instance
Writing a bean for a feature class
Package com.wisely.highlight_spring4.ch1.javaconfig;
1
public class Functionservice {
public string SayHello (string word) {
"Hello" + Word +
}
}
Code explanation
The @service declaration bean is not used here.
Beans using the Feature class
Package com.wisely.highlight_spring4.ch1.javaconfig;
Import Com.wisely.highlight_spring4.ch1.javaconfig.FunctionService;
1
public class Usefunctionservice {
2
Functionservice Functionservice;
Public void Setfunctionservice (Functionservice functionservice) {
This . functionservice = Functionservice;
}
Public string SayHello (string word) {
return Functionservice.sayhello (word);
}
}
Code explanation
@service declaration Bean is not used here
The bean is not injected here using @autowired annotations
Configuration class
Package com.wisely.highlight_spring4.ch1.javaconfig;
ImportOrg.springframework.context.annotation.Bean;
ImportOrg.springframework.context.annotation.Configuration;
@Configuration1
public class Javaconfig {
@Bean //2
Public Functionservice Functionservice () {
return new Functionservice ();
}
@Bean
Public Usefunctionservice Usefunctionservice () {
Usefunctionservice Usefunctionservice = new Usefunctionservice ();
Usefunctionservice.setfunctionservice (Functionservice ()); //3
return usefunctionservice;
}
}
Code explanation
Using @configuration annotations indicates that the current class is a configuration class, which means that there may be 0 or more @bean annotations in this class, and no packet scanning is used here, because so the beans are defined in this class.
Use @bean annotations to declare the return value of the current method Functionservice is a Bean,bean name is the method name.
Functionservice () is called directly when the bean is injected into the functionservice.
Package com.wisely.highlight_spring4.ch1.javaconfig;
Import Org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void Main (string[] args) {
Annotationconfigapplicationcontext context =
New Annotationconfigapplicationcontext (Javaconfig. Class);
Usefunctionservice Usefunctionservice = Context.getbean (usefunctionservice. Class);
Functionservice Functionservice = Context.getbean (functionservice. Class);
System. out.println (Usefunctionservice.sayhello ("java config"));
System. out.println (Functionservice.sayhello ("Faunjoe"));
Context.close ();
}
}
Spring Java Configuration