0

I can't get spring working. Somebody can help me? In this sample I have 2 autowiring: CvDaoImpl and SessionFactory. Both doesn't recognized in context and returns null.

CvQuery.java

package service;

@Service
public class CvQuery {

    @Autowired
    private CvDao cvDao;

    private String query;

    @Transactional
    public List<User> getUsers() {
        return cvDao.getUsers(query);
    }
...getters/setters
}

CvDaoImpl.java

package dao;

@Repository
public class CvDaoImpl implements CvDao {

    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public List<User> getUsers(String query) {
        Session currentSession = sessionFactory.getCurrentSession();
        Query<User> theQuery = currentSession.createQuery(query, User.class);
        return theQuery.getResultList();
    }

    public CvDaoImpl() {
    }
}

And here is configuration.

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>test</groupId>
    <artifactId>test</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
<dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.2.3.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.3.RELEASE</version>
        </dependency>
...
    </dependencies>
</project>

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

    <context:component-scan base-package="configuration"/>
    <context:component-scan base-package="entity"/>
    <context:component-scan base-package="dao"/>
    <context:component-scan base-package="service"/>
    <context:annotation-config/>
</beans>

DataSourseConfig.class

package configuration;

@Configuration
@PropertySource("classpath:persistence-mysql.properties")
@EnableTransactionManagement
public class DataSourceConfig {

    @Autowired
    private Environment env;

    @Bean
    public LocalSessionFactoryBean sessionFactory() {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource(dataSource());
        sessionFactory.setPackagesToScan("entity");
        sessionFactory.setHibernateProperties(hibernateProperties());
        return sessionFactory;
    }
}

I'm using intellij idea so if configuration is correct, maybe some settings in project structure are wrong. spring_config.xml is attached as spring application context on Modules section.

Added:

    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring_config.xml");
        ....
    }

application context should be create here

Upd I've tried to use objects from simple Java classes and from beans. Different contexts - that was a problem.

  • How are you creating the application context ? Please share the class with the `main()` method – R.G Feb 08 '20 at 16:31
  • Remove the useless constructor of CvDaoImpl.java – SamwellTarly Feb 08 '20 at 17:08
  • Added main method with application context defining. As i understand, context will automaticaly gets all beans from Am I wrong? Previously I've tried similar operations with MVC and Tomcat an everything was good. It looks like I don't understand how context works and what is visibility range – Aliaksei Lasevich Feb 08 '20 at 18:05
  • With Tomcat it's the servlet into the web.xml which start the context creation, here as @R.G said if you are in a simple java application you need to start the context from the main method. – adrien olivier Feb 08 '20 at 20:17
  • 1
    I think you should not add multiple lines of component scans. if you go for annotation based config anyway you could use an annotation for that too. see here: https://stackoverflow.com/questions/5269450/multiple-packages-in-contextcomponent-scan-spring-config – wemu Feb 08 '20 at 20:26
  • As @wemu mentioned , modify the component scan annotation to `` . Also I assume you are getting `CvQuery` instance from the application context and not using `new` keyword. – R.G Feb 09 '20 at 00:13

1 Answers1

0

You probably haven't enabled annotation scanning.

Have a look at this for your application entry point.

Another way to do it would be to declare a configuration class and make use of the @Bean annotation to explicity create your bean.

Adam
  • 1,724
  • 13
  • 16