关于Spring注入取值为null的说明
?
前言:
在SSH组合中,用到了JDBC连接,需要给一个类中注入JdbcTemplate,注入后,在其他地方调用的时候,报空指针异常。
后来,写了个小实例,也是SSH环境,只有几个类,结构如下。
<!--StartFragment -->

<bean id="dataSource"destroy-method="close"><property name="driverClassName"value="net.sourceforge.jtds.jdbc.Driver"></property><property name="url" value="jdbc:jtds:sqlserver://localhost:1433/mytest"></property><property name="username" value="sa"></property><property name="password" value=""></property></bean>?
<bean id="jdbcTemplate" /></property></bean><bean id="myDb" lazy-init="default" autowire="default"><property name="jdbcTemplate"><ref bean="jdbcTemplate" /></property><property name="strAbc"><value>57ui</value></property><!-- <constructor-arg index="0"><ref bean="jdbcTemplate" /></constructor-arg><constructor-arg index="1"><value>8uiw</value></constructor-arg> --></bean>?MyDB的代码
package com.mini;import java.sql.Connection;import java.sql.SQLException;import javax.sql.DataSource;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.jdbc.datasource.DataSourceUtils;public class MyDB {private Connection conn;private JdbcTemplate jdbcTemplate;private String strAbc;//private DataSource dataSource;//public DataSource getDataSource() {//return dataSource;//}////public void setDataSource(DataSource dataSource) {//this.dataSource = dataSource;//}public JdbcTemplate getJdbcTemplate() {return jdbcTemplate;}public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {this.jdbcTemplate = jdbcTemplate;try {conn = jdbcTemplate.getDataSource().getConnection();System.out.println("------- " + conn.getCatalog());} catch (SQLException e) {e.printStackTrace();}System.out.println("............ jdbcTemplate : " + jdbcTemplate.getDataSource().toString());}public MyDB() {}public MyDB(JdbcTemplate jdbcTemplate, String strAbc) {this.jdbcTemplate = jdbcTemplate;this.strAbc = strAbc;}public void getConn() {System.out.println(">>>>>> get >> str >>>> " + strAbc);conn = DataSourceUtils.getConnection(jdbcTemplate.getDataSource());try {System.out.println(">>>>>>>>>>>> " + conn.getCatalog());} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public String getStrAbc() {return strAbc;}public void setStrAbc(String strAbc) {this.strAbc = strAbc;System.out.println(">>>>>> set >> str >>>> " + this.strAbc);}}?而在Action中调用代码为public ActionForward dbconn(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {MyDB mydb = new MyDB();mydb.getConn();//MyDB mydb = (MyDB) getLogicBean("myDb");//mydb.getConn();return mapping.findForward("moving");}??测试setter方式注入,报空指针,后来换为构造方式注入,还是如此。到最后发现,原来我调用getConn()的时候,是new的一个新对象,不是从spring上下文中取的,问题就出现在这里。我spring整合struts是通过插件方式植入的,这里由下面的代码可以取到我所需要的:public Object getLogicBean(String logicBean) { WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServlet().getServletContext()); return wac.getBean(logicBean);}?在struts中关键配置<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"><set-property property="contextConfigLocation" value="classpath:/applicationContext.xml" /></plug-in>?最后,在Action中通过这样的方式调用就没有问题了
public ActionForward dbconn(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {//MyDB mydb = new MyDB();//mydb.getConn();MyDB mydb = (MyDB) getLogicBean("myDb");mydb.getConn();return mapping.findForward("moving");}?