Bean的注入方式
set注入
基本数据类型注入
集合注入
对象注入
构造注入
基本数据类型注入
自动装配
<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" default-autowire="byType" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <bean id="userBiz1" class="com.javaxl.ioc.biz.impl.UserBizImpl1"></bean> <bean id="userBiz2" class="com.javaxl.ioc.biz.impl.UserBizImpl2"></bean> <!-- IOC注入 --> <bean id="userAction" class="com.javaxl.ioc.web.UserAction"> <!-- <property name="sid" value="22"></property> --> <!-- <property name="sname" value="zs"></property> --> <constructor-arg name="sid" value="22"></constructor-arg> <constructor-arg name="sname" value="zs"></constructor-arg> <property name="hobby"> <list> <value>篮球</value> <value>足球</value> <value>读书</value> </list> </property> <!-- 项目中的Bean交给spring进行管理,由原来的程序员维护,变成spring维护,方便项目的后期维护,这就是控制反转 --> <!-- <property name="userBiz" ref="userBiz2"></property> --> </bean> </beans>
package com.javaxl.ioc.util;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
@WebListener
public class SpringLoadListener implements ServletContextListener {
private String springXmlLocation = "";
@Override
public void contextInitialized(ServletContextEvent sce) {
ServletContext servletContext = sce.getServletContext();
springXmlLocation = servletContext.getInitParameter("springXmlLocation");
if(null == springXmlLocation || "".equals(springXmlLocation)) {
springXmlLocation = "/spring-context.xml";
}
System.out.println("spring的主配置文件是:"+springXmlLocation);
ApplicationContext springContext = new ClassPathXmlApplicationContext(springXmlLocation);
servletContext.setAttribute("SPRING_CONTEXT_KEY", springContext);
}
}
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>springXmlLocation</param-name>
<param-value>/spring-other.xml</param-value>
</context-param>
</web-app>
package com.javaxl.ioc.test;
import java.io.IOException;
import javax.servlet.ServletContext;
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 com.javaxl.ioc.web.UserAction;
/**
* 测试一下spring的上下文是否交给了tomcat容器进行管理
* @author Administrator
*
*/
@WebServlet("/iocServlet")
public class IocServlet extends HttpServlet {
private static final long serialVersionUID = -3937540355899886901L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext servletContext = req.getServletContext();
ApplicationContext springContext = (ApplicationContext) servletContext.getAttribute("SPRING_CONTEXT_KEY");
UserAction bean = (UserAction) springContext.getBean("userAction");
bean.test3();
}
}

备案号:湘ICP备19000029号
Copyright © 2018-2019 javaxl晓码阁 版权所有