Announcement Announcement Module
Collapse
No announcement yet.
Unable to get the spring bean. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Unable to get the spring bean.

    Hi, im using Spring 3.0.6.Release and Apache CXF 2.5.1 in my application, and my application context is loaded in the web.xml. After I my application container jetty is started up, all the beans are initialized. But when I create a LoggingDao Objec the JdbcTemplate is not set. Did I forget something? Have anyone a idea why dependency injection not work?

    Create a LoggingDao Object.
    Code:
    private LoggingDao loggingDao = new LoggingDao();
    web.xml
    HTML Code:
    	<listener>
    		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    	</listener>
    
    	<context-param>
    		<param-name>contextConfigLocation</param-name>
    		<param-value>classpath:application-context.xml</param-value>
    	</context-param>
    application-context.xml
    Code:
    	<bean id="org.h2.tools.Server" class="org.h2.tools.Server"
    		factory-method="createTcpServer" init-method="start" destroy-method="stop">
    		<constructor-arg value="-tcp,-tcpAllowOthers,-tcpPort,8043" />
    	</bean>
    
    	<bean id="org.h2.tools.Server-WebServer" class="org.h2.tools.Server"
    		factory-method="createWebServer" init-method="start">
    		<constructor-arg value="-web,-webAllowOthers,-webPort,8082" />
    	</bean>
    
    	<bean id="H2DatabaseJDBCDriver" class="org.h2.Driver" scope="singleton"
    		init-method="load" depends-on="org.h2.tools.Server" />
    
    	<bean id="dataSource"
    		class="org.springframework.jdbc.datasource.DriverManagerDataSource"
    		depends-on="org.h2.tools.Server">
    		<property name="driverClassName" value="org.h2.Driver" />
    		<property name="url" value="jdbc:h2:tcp://localhost:8043/~/xserver" />
    		<!-- ;TRACE_LEVEL_FILE=3;TRACE_LEVEL_SYSTEM_OUT=3 -->
    		<property name="username" value="sa" />
    		<property name="password" value="" />
    	</bean>
    
    	<bean id="loggingDao" class="com.foo.LoggingDao">
    		<property name="jdbcTemplate" ref="jdbcTemplate" />
    	</bean>
    
    	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate ">
    		<property name="dataSource" ref="dataSource" />
    	</bean>
    LoggingDao
    Code:
    package com.foo;
    
    public class LoggingDao {
    
    	private JdbcTemplate jdbcTemplate;
    
    	public LoggingDao() {}
    
    	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
    		this.jdbcTemplate = jdbcTemplate;
    		if(jdbcTemplate!=null) {
    			System.out.println("JDBC Template is not null");
    		}
    	}

  • #2
    Unable to get the spring bean.

    private LoggingDao loggingDao = new LoggingDao();

    I don't think Spring container will inject if you do this. You have to either annotate it with @Autowired(for this you have to enable annotation in your applicationContext.xml).

    Else, you have first get the spring context and then get the DAO bean from it.

    Hope this helps.

    Comment

    Working...
    X