Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8664839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:20:12+00:00 2026-06-12T17:20:12+00:00

I got stuck right now. Firstly i made Java application that i run from

  • 0

I got stuck right now.

Firstly i made Java application that i run from console and is annotation based configuration.
CONFIGURATION BELOW WORKS WHEN RUNNING FROM CONSOLE configuration is in config package

@Configuration
public class JpaConfiguration {

  @Value("#{dataSource}")
  private javax.sql.DataSource dataSource;


  @Bean
  public Map<String, Object> jpaProperties() {
    Map<String, Object> props = new HashMap<String, Object>();
    props.put("hibernate.dialect", MySQL5Dialect.class.getName());
    props.put("javax.persistence.validation.factory", validator());
    props.put("hibernate.ejb.naming_strategy", ImprovedNamingStrategy.class.getName());
    return props;
  }

  @Bean
  public LocalValidatorFactoryBean validator() {
    return new LocalValidatorFactoryBean();
  }

  @Bean
  public JpaVendorAdapter jpaVendorAdapter() {
    HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
    hibernateJpaVendorAdapter.setGenerateDdl(true);
    hibernateJpaVendorAdapter.setDatabase(Database.MYSQL);
    hibernateJpaVendorAdapter.setShowSql(false);
    return hibernateJpaVendorAdapter;
  }

  @Bean
  public PlatformTransactionManager transactionManager() {
    return new JpaTransactionManager(
        localContainerEntityManagerFactoryBean().getObject());
  }

  @Bean
  public LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean() {
    LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean();
    lef.setDataSource(this.dataSource);
        lef.setPackagesToScan("domain");
    lef.setJpaPropertyMap(this.jpaProperties());
    lef.setJpaVendorAdapter(this.jpaVendorAdapter());
    return lef;
  }
}

@Configuration
@ImportResource("classpath:root-context.xml")
@PropertySource("classpath:database.properties")
public class DataSourceConfig {

    public DataSourceConfig() {}

}

Here is my root-context.xml which is in src/main/resources package

<?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:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.1.xsd
        http://www.springframework.org/schema/jdbc 
        http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
        http://www.springframework.org/schema/data/jpa 
        http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

    <context:property-placeholder location="classpath:database.properties" />

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close" 
        p:driverClass="com.mysql.jdbc.Driver"
        p:jdbcUrl="${jdbc.url}" 
        p:user="${jdbc.username}" 
        p:password="${jdbc.password}"
        p:initialPoolSize="0" 
        p:minPoolSize="0" 
        p:maxPoolSize="10"
        p:maxIdleTime="300" />

    <jpa:repositories base-package="domain" />
    <tx:annotation-driven transaction-manager="transactionManager" />
</beans>

My main method

public class ConsoleRun {

    public static void main(String[] args) throws Exception {

        final Logger log = LoggerFactory
                .getLogger("ConsoleRun");

        log.info("Starting application");

        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
        ctx.scan("*");
        ctx.refresh();

        TestDao testDao = ctx.getBean(TestDao.class);
        testDao.testUsersList();

        log.info("========GETTING ALL RESULTS==============");
        testDao.testResultsList();
    }
}

My Service class for accessing DAOs which is in console package

@Service
public class TestDao {

    static final Logger log = LoggerFactory.getLogger("TestDatabase");

    @Autowired
    private UserDao userDao;

    @Autowired
    private ResultDao resultDao;

    @Autowired
    private GameDao gameDao;

    public List<User> testUsersList() {
        log.info("Getting all users");
        List<User> users = userDao.findAll();
        for (User u : users) {
            log.info("User: {}", u);
        }
        return users;
    }


    public void testResultsList() {
        List<Result> results = resultDao.findAll();
        for (Result r : results) {
            log.info("Result: {}", r);
        }
    }

    public User findUserById(Long id) {
        return userDao.findById(id);
    }
}

ABOVE CODE WORKS WHEN STARTING FROM CONSOLE

BELOW CODE DOESN’T WORK

Now i have an issue when i want to run it in Tomcat container. I’m trying different ways to configure it.
How could I reuse my JpaConfiguration class and root-context.xml for Tomcat?

This is what i currently have in my web.xml

<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>Restful Web Application</display-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/app-config.xml</param-value>
    </context-param>

    <servlet>
        <servlet-name>jersey-serlvet</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>api</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>jersey-serlvet</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>

</web-app>

My app-config.xml

<?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:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                            http://www.springframework.org/schema/jdbc
                            http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
                            http://www.springframework.org/schema/tx
                            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
                            http://www.springframework.org/schema/context
                            http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <jpa:repositories base-package="domain" />
    <context:component-scan base-package="domain,api,config" />

    <!-- Weaves in transactional advice around @Transactional methods -->
    <tx:annotation-driven transaction-manager="transactionManager" />

    <bean id="dataSource" class="config.DataSourceConfig" />
    <bean id="JpaConfiguration" class="config.DataSourceConfig" />

</beans>

Here is the main problem where userDao doesn’t autowire and throws nullpointer exception Authresource is in api package

@Component
@Path("/auth")
public class AuthResource {

    @Autowired
    UserDao userDao;

    @Autowired
    TestDao testDao;

    @GET
    @Path("/users")
    @Produces(MediaType.APPLICATION_JSON)
    public List<User> getAllUsers() {

        return userDao.findAll();
    }
}

My rest is working, I have simple REST service class that works on url localhost:8080/application/rest/hello/message

@Path("/hello")
public class HelloResource {
    @GET
    @Path("/{param}")
    public Response getMsg(@PathParam("param") String msg) {

        String output = "Jersey say : " + msg;
        return Response.status(200).entity(output).build();
    }
}

Is there a possibility for tomcat to load configuration directly from java config file? That is what I have in app-config.xml <context:component-scan base-package="domain,api,config" />
Where else could be the problem and why I’m getting nullpointer exception for userDao in AuthResource class?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T17:20:16+00:00Added an answer on June 12, 2026 at 5:20 pm

    I think you need to add the ContextLoaderListener to your web.xml

    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm stuck. (got a thinking barrier right now) :/ I need a stringarray from
Got stuck here: http://jsfiddle.net/UFkg8/ Right now the animation is top-down. What do I need
I'm learning Ruby right now and I got stuck, maybe you guys can help
I've recently started learning OpenGl through the SB fifth edition, unfortunately got stuck right
I've got a little game that a friend and I are writing in Java.
I'm developing a web application based on Hibernate, Spring and Wicket. Until now I
I'm trying to get data from a server. Right now, I am polling the
I have an application that's a mix of Java and C++ on Solaris. The
Background: I have a kubuntu laptop right now that I can't use wirelessly, i.e.
I'm having some trouble with MySql right now. I have an query that works

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.