spring中通過配置文件注入的方法
通過配置文件注入的方法
馬克-to-win:上面的注入方法是通過@Service的注解方法。類似的還有@Repository、@Component、 @Constroller,功能大體一樣,就是實例化以后放到Spring容器當(dāng)中接受管理。當(dāng)然你肯定樂意在service類前放@Service而不愿意放@Repository而故意迷惑自己。另外注意,缺省的情況都是單態(tài)的。(省我們事了,但要注意線程安全)。除了注解注入,我們還有配置文件的方法來注入。相比注解的方法來講,配置文件的方法比較集中,但缺乏靈活性。怎么講呢?a處和b處想按不同的方式來處理?不行。因為統(tǒng)一一個地方處理。a和b 必須統(tǒng)一,所以缺少了靈活性。馬克- to-win:馬克 java社區(qū):防盜版實名手機尾號: 73203
例 1.2
package com;
import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.support.RequestContextUtils;
import service.interfac.ILoginService;
@Controller
public class HelloWorldController {
private ILoginService loginServic;
@RequestMapping("/helloa")
public ModelAndView helloWorld(HttpServletRequest request, HttpServletResponse response,
HttpSession sesssion) {
ServletContext sc=RequestContextUtils.getWebApplicationContext(request).getServletContext();
WebApplicationContext wac=WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
ILoginService loginServic=(ILoginService)wac.getBean("loginService");
loginServic.login();
System.out.println("after loginServic.login()");
return new ModelAndView("/helloq", "message", "你好");
}
}
注意上面Controller中的WebApplicationContext wac=WebApplicationContextUtils.getRequiredWebApplicationContext(sc);這句話要想工作,必須在web.xml中加入ContextLoaderListener,因為ServletContext是我們在jsp那時學(xué)的,是 Tomcat提供的一個最大的范圍,存放鍵值對。而WebApplicationContext是Spring提供的一個工具,去配置文件中查找 bean。配置文件在哪?它需要知道.怎么知道?在web.xml中的ContextLoaderListener的 contextConfigLocation參數(shù)。
在web.xml中加入下面的語句:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
在spring-servlet.xml中加入如下的內(nèi)容:
<bean id="NiutDAO" class="com.NiutDAO">
</bean>
<bean id="loginService" class="service.LoginServiceImpl" >
<property name="niutDAO">
<ref bean="NiutDAO" />
</property>
</bean>
package service;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;
import com.NiutDAO;
import service.interfac.ILoginService;
public class LoginServiceImpl implements ILoginService {
NiutDAO niutDAO;
// public NiutDAO getNiutDAO() {
// return niutDAO;
// }
/*本例中沒有下面的setter不能工作*/
public void setNiutDAO(NiutDAO niutDAO) {
this.niutDAO = niutDAO;
}
public void login() {
System.out.println("LoginServiceImpl");
}
/*需要在src目錄中加入applicationContext.xml,才能用main來測試
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="NiutDAO" class="com.NiutDAO">
</bean>
<bean
id="loginService"
class="service.LoginServiceImpl" >
<property name="niutDAO">
<ref bean="NiutDAO" />
</property>
</bean>
</beans>
*/
public static void main(String[] args)
{
ClassPathXmlApplicationContext cp= new ClassPathXmlApplicationContext("applicationContext.xml");
ILoginService ls=(ILoginService)cp.getBean("loginService");
ls.login();
}
}
package com;
public class NiutDAO {
}