Spring框架context的注解管理方法之二,使用注解注入对象属性

Spring框架context的注释管理方法之二 使用注解注入对象属性,springcontext

先是还是xml的配置文件

<?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-context.xsd"> 
      <!--  开启注解扫描  -->
     <context:component-scan base-package="com.swift"></context:component-scan>
</beans>

接着是假定dao的类

package com.swift;

import org.springframework.stereotype.Component;

@Component(value="dao")
public class Dao {
    public String fun() {
        return "This is Dao's fun()........";

    }
}

变动三个对象很有利,以致@Component(value=”dao”)中的value=都得以不写,造成

@Component(“dao”)

然后是假定service的类

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()......."+"rn"+this.dao.fun();
    }
    //注意使用注解方法,不需要自己生成setter方法了
    public void setDao(Dao dao) {
        this.dao = dao;
    }

}

与安排文件中选用<bean id=”service”
class=”com.swift.Service”><property name=”dao”
ref=”dao”></property></bean>

不等,证明生成八个对象后,再注脚属性

@Autowired

就化解了,自动装配,自动连线

最后应用Servlet来测量检验一下

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);
    }

}

浏览器结果如下

永利澳门游戏网站 1

电动装载的这种艺术   @Autowired  
 原理是由此类名找到定义的对象,这种注明使用十分的少,因为多个对象存在的话,注入的是哪些?

所以,使用

另二个讲明,能够一清二楚到底注入哪个指标

@Resource(name=”dao”)
private Dao dao;

这种措施运用很多

 

使用表明注入对象属性,springcontext 首先依旧xml的铺排文件 ? xml
version=”1.0″ encoding=”UTF-8″ ? beans xmlns =”ht…

第一依旧xml的安排文件

<?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-context.xsd"> 
      <!--  开启注解扫描  -->
     <context:component-scan base-package="com.swift"></context:component-scan>
</beans>

接着是假定dao的类

package com.swift;

import org.springframework.stereotype.Component;

@Component(value="dao")
public class Dao {
    public String fun() {
        return "This is Dao's fun()........";

    }
}

转换一个指标很方便,乃至@Component(value=”dao”)中的value=都得以不写,产生

@Component(“dao”)

然后是假定service的类

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()......."+"rn"+this.dao.fun();
    }
    //注意使用注解方法,不需要自己生成setter方法了
    public void setDao(Dao dao) {
        this.dao = dao;
    }

}

永利澳门游戏网站,与配置文件中动用<bean id=”service”
class=”com.swift.Service”><property name=”dao”
ref=”dao”></property></bean>

区别,表明生成两个目的后,再表明属性

@Autowired

发表评论

电子邮件地址不会被公开。 必填项已用*标注