Announcement Announcement Module
Collapse
No announcement yet.
Can I call Spring.getBean outside of the container??? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Can I call Spring.getBean outside of the container???

    I'm calling Spring.getBean like this today;

    DataSource _dataSource = (DataSource)Spring.getBean("DB_CONTEXT_NAME");


    Everything works fine in my WebLogic application server.

    However, I want to be able to instantiate my application outside of the application server. Can I do this without changing my code by providing some alternate parameters on the command line or an alternate configuration file of some sort?

  • #2
    Do you mean Spring beans in general or only your data source bean?
    If the latter is the case, I guess you are performing a JNDI lookup to get the data source?

    It's possible to create a DriverManagerDataSource if you need one outside a J(2)EE container (only for testing purposes) or use Apache Commons DBCP.

    --
    Dries

    Comment


    • #3
      Performing a JNDI lookup outside a J(2)EE container

      Thanks! I do mean my data source bean only. I am performing a JNDI lookup to get the data source and I need to be able to lookup the data source outside a J(2)EE container for testing purposes...and not change any existing java code. Can this be done?

      Comment


      • #4
        Yes this can be done.
        There are different strategies to achieve this. You could e.g. put the bean definition of your test data source and production data source in different Spring configuration files. Depending on the environment you load / import the correct file.

        edit:
        I've found an interesting post from Paul Newport regarding this. It seems that JndiObjectFactoryBean has a fall back strategy by setting the property 'defaultObject'.

        --
        Dries
        Last edited by driesva; Sep 29th, 2008, 03:35 PM.

        Comment


        • #5
          Originally posted by driesva View Post
          I've found an interesting post from Paul Newport regarding this.
          Dries
          Thanks, that saved me searching for that link !

          Also, you bernardbjr, you really shouldnt be doing a getBean within your code.

          Your class that is using the datasource should have the datasource injected into it by Spring, so within your code you just have a private datasource field with a setter on it, and then you make your code that is using the datasource a Spring bean.

          Also, it's rare in Spring nowadays to have to use a datasource directly at all. Have a look at the JDBC section of the manual. There are a lot of Spring classes that hide datasource and connection handling from you, so you just concentrate on coding the data access bits.

          for example, look at the code below, the jdbcTemplate is autowired in, the class below is a DAO Spring bean because I have annotated it as such with @Repository, and the jdbcTemplate itself is defined in an application context with the datasource wired in to it there. As you can see, not a datasource in sight in the code itself:
          Code:
          package blog.jdbc.dao;
          
          import java.util.List;
          
          import org.springframework.beans.factory.annotation.Autowired;
          import org.springframework.beans.factory.annotation.Qualifier;
          import org.springframework.jdbc.core.BeanPropertyRowMapper;
          import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
          import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
          import org.springframework.jdbc.core.namedparam.SqlParameterSource;
          import org.springframework.stereotype.Repository;
          
          import blog.jpa.domain.Restaurant;
          import blog.jpa.domain.RestaurantDao;
          @Repository
          @Qualifier("JdbcRestaurantImpl")
          public class JdbcRestaurantDao  implements RestaurantDao {
          	
          	
          	@Autowired
          	private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
          
          	@SuppressWarnings("unchecked")
          	public List<Restaurant> findByName(String name) {
          		
          		SearchParameters search = new SearchParameters();
          		search.setName(name);
          		SqlParameterSource namedParameters = new BeanPropertySqlParameterSource(search);
          		
          		return  namedParameterJdbcTemplate.query(
          			    "select id,name,address_id from restaurant where name = :name",
          			    namedParameters,new BeanPropertyRowMapper(Restaurant.class)
          			    );
          	}
          
          }
          NB, the above code has a direct relationship with the code here (just nodding my hat):

          http://blog.springsource.com/2006/05...-in-spring-20/

          I was giving a course on persistence and did the same thing three times, once with JDBC, once with Hibernate, and once with JPA.

          Comment

          Working...
          X