Annotation management method 2 of Spring framework context uses annotation injection object attributes, springcontext
First, it is the xml configuration file.
<? Xml version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: context = "http://www.springframework.org/schema/context" xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-c Ontext. xsd "> <! -- Enable annotation scanning --> <context: component-scan base-package = "com. swift"> </context: component-scan> </beans>
NextIt is assumed that the dao class
package com.swift;import org.springframework.stereotype.Component;@Component(value="dao")public class Dao { public String fun() { return "This is Dao's fun()........"; }}
It is very convenient to generate an object. Even the value = in @ Component (value = "dao") can be left empty and changed
@ Component ("dao ")
ThenIt is assumed that the service class
Package com. swift; import org. springframework. beans. factory. annotation. autowired; import org. springframework. stereotype. component; @ Component (value = "service") public class Service {@ Autowired private Dao dao; public String fun () {return "This is Service's fun ()....... "+" \ r \ n "+ this. dao. fun () ;}// note that you do not need to generate the setter method by yourself. public void setDao (Dao dao) {this. dao = dao ;}}
Use <bean id = "service" class = "com. swift. service "> <property name =" dao "ref =" dao "> </property> </bean>
Different, annotation generates two objects, and then annotation attributes
@ Autowired
It's done, automatic assembly, and automatic connection.
LastUse Servlet to test
package com.swift;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;@WebServlet("/test")public class ServletTest extends HttpServlet { private static final long serialVersionUID = 1L; public ServletTest() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().append("Served at: ").append(request.getContextPath()); ApplicationContext context=new ClassPathXmlApplicationContext("zhujie.xml"); Service service=(Service) context.getBean("service"); String test=service.fun(); response.getWriter().append(test); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }}
The result of the browser is as follows:
This method of automatic loading @ Autowired principle is to find the defined object through the class name. This annotation is not used much, because if multiple objects exist, which one is injected?
Therefore, use
Another Annotation can specify which object to inject
@ Resource (name = "dao ")
Private Dao;
This method is widely used.