Spring's zero configuration

1 What is zero configuration
We do not use any Spring configuration file to configure any Bean instance, use Spring to automatically search for Java classes under certain paths, and register these Java classes as Bean instances.


2 Annotation adopted by Spring
@Component annotates ordinary JavaBean instances
@Controller annotates a controller component class
@Service annotates a business logic component class
@Repository annotates a DAO component class
Try to use @Controller, @Service, @Repository to annotate these bean classes, these bean classes will be treated as special JavaEE components, better handled by tools, or associated with aspects.
3 How to use
1. You need to specify the search path in Spring's configuration file. Spring will automatically search for all Java classes in the path and create Bean instances based on these Java classes.
<!-- Automatically scan and wire beans -->
<context:component-scan base-package="cn.jiaotong.oa"></context:component-scan>
2、@Controller
   @Scope("prototype")
public class HomeAction {
}
3. Use @Resource to configure dependencies
@Resource
public HomeAction homeAction;
4 @Resource
Use @Resource to create collaborator bean for target bean
5 Autowiring vs Precise Wiring
Using @Autowired to specify autowiring, you can annotate setter methods, normal methods, and constructors.

Where @Autowired can use it to annotate normal methods with multiple parameters.

Autowiring can be specified using @Qualifier to allow bean identifiers.















Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325579554&siteId=291194637