Spring framework annotation and configuration file mixed use (generate object and property injection) xml configuration file does not prompt solution, springannotation

Source: Internet
Author: User

Spring framework annotation and configuration file mixed use (generate object and property injection) xml configuration file does not prompt solution, springannotation

UseConfiguration File xml generation object

UseAnnotation injection attributes

Create two types of dao: StudentDao. class and CourseDao. class.

Create a service class Service. class

Finally, the test is performed through a Servlet class ServletTest. class.

The code of the StudentDao. class is as follows:

package com.swift;public class StudentDao {    public String fun() {        return "This is StudentDao's fun()........";    }}

The CourseDao. class code is as follows:

package com.swift;public class CourseDao {    public String fun() {        return "This is CourseDao's fun().......";    }}

The code for the Service. class is as follows:

package com.swift;import javax.annotation.Resource;public class Service {    @Resource(name="studentDao")    private StudentDao studentDao;    @Resource(name="courseDao")    private CourseDao courseDao;    public String fun() {        return "This is Service's fun()........."+this.studentDao.fun()+this.courseDao.fun();    }}

The class attributes are directly injected with the annotation method. This object is generated in the xml configuration file. The configuration file code is as follows:

<? 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> <bean id =" studentDao "class =" com. swift. studentDao "> </bean> <bean id =" courseDao "class =" com. swift. courseDao "> </bean> <bean id =" service "class =" com. swift. service "> </bean> </beans>

The code of the ServletTest. class is as follows:

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("beanZhujie.xml");        Service service=(Service) context.getBean("service");        response.getWriter().append(service.fun());    }    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        doGet(request, response);    }}

The browser result is as follows:

 

No solution is prompted for the xml configuration file

Window --> preferences --> Search xml catalog --> add --> enter constraints in the key URL http://www.springframework.org/schema/beans/spring-beans.xsd

--> In location, click file system --> Find the spring framework package --> schema --> beans --> the last version --> select schema location from the key type drop-down menu.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.