Spring Autowire fails with No qualifying bean of type found for dependency error - spring

Here's my scenario. My project is using Spring (3.2.3.RELEASE), Struts2 (2.3.14.3) and JPA (2.0). We have a project (let's call it project A) that contains various entities and common classes. We use project A to generate a .jar file so that other projects can use these classes. In project A we've used the Spring stereotypes: #Component. #Service or #Repository for those classes we want Spring to inject. Whenever we try to inject beans from the jar, we get an error similar to:
No qualifying bean of type
[com.ceiwc.bc.commonsql.service.CommonSQLService] found for
dependency: expected at least 1 bean which qualifies as autowire
candidate for this dependency. Dependency annotations:
{#org.springframework.beans.factory.annotation.Autowired(required=true)}
Could not autowire field: private com.ceiwc.bc.commonsql.service.CommonSQLService
com.ceiwc.ma.mvc.action.LoginAction.commonSQLService; nested exception
is org.springframework.beans.factory.NoSuchBeanDefinitionException: No
qualifying bean of type
[com.ceiwc.bc.commonsql.service.CommonSQLService] found for
dependency: expected at least 1 bean which qualifies as autowire
candidate for this dependency. Dependency annotations:
{#org.springframework.beans.factory.annotation.Autowired(required=true)}
Error creating bean with name 'com.ceiwc.ma.mvc.action.LoginAction': Injection of autowired
dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Could not
autowire field: private
com.ceiwc.bc.commonsql.service.CommonSQLService
com.ceiwc.ma.mvc.action.LoginAction.commonSQLService; nested exception
is org.springframework.beans.factory.NoSuchBeanDefinitionException: No
qualifying bean of type
[com.ceiwc.bc.commonsql.service.CommonSQLService] found for
dependency: expected at least 1 bean which qualifies as autowire
candidate for this dependency. Dependency annotations:
{#org.springframework.beans.factory.annotation.Autowired(required=true)}
Unable to instantiate Action, com.ceiwc.ma.mvc.action.LoginAction, defined for 'doLogin' in namespace '/Login'Error creating bean with
name 'com.ceiwc.ma.mvc.action.LoginAction': Injection of autowired
dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Could not
autowire field: private
com.ceiwc.bc.commonsql.service.CommonSQLService
com.ceiwc.ma.mvc.action.LoginAction.commonSQLService; nested exception
is org.springframework.beans.factory.NoSuchBeanDefinitionException: No
qualifying bean of type
[com.ceiwc.bc.commonsql.service.CommonSQLService] found for
dependency: expected at least 1 bean which qualifies as autowire
candidate for this dependency. Dependency annotations:
{#org.springframework.beans.factory.annotation.Autowired(required=true)}
We are using Java configuration for Spring. Here is our configuration class:
package com.zzz.bc.config;
#Configuration
#ImportResource({"/WEB-INF/spring/spring-config.xml"})
#ComponentScan(basePackages = "com.zzz")
public class ApplicationConfig {
#Bean
public JavaMailSender mailSender() {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost(${app.mail.host});
return mailSender;
}
}
package com.zzz.bc.config;
#Configuration
#EnableTransactionManagement
#Import(ApplicationConfig.class)
#PropertySource({"classpath:db.properties"})
public class DataConfig {
#Autowired
Environment environment;
#Bean
public PlatformTransactionManager transactionManager() {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactoryBean().getObject());
return transactionManager;
}
#Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean() {
String[] packagesToScan =
{environment.getProperty("db.packagesToScan1"), environment.getProperty("db.packagesToScan2")};
Map<String, Object> jpaProperties = new HashMap<String, Object>();
jpaProperties.put("eclipselink.weaving", "false");
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
entityManagerFactoryBean.setDataSource(dataSource());
entityManagerFactoryBean.setPackagesToScan(packagesToScan);
entityManagerFactoryBean.setLoadTimeWeaver(new InstrumentationLoadTimeWeaver());
entityManagerFactoryBean.setJpaVendorAdapter(vendorAdapter());
entityManagerFactoryBean.setJpaPropertyMap(jpaProperties);
return entityManagerFactoryBean;
}
#Bean
public DriverManagerDataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(environment.getProperty("db.driver"));
dataSource.setUrl(environment.getProperty("db.url"));
dataSource.setUsername(environment.getProperty("db.username"));
dataSource.setPassword(environment.getProperty("db.password"));
return dataSource;
}
#Bean
public JpaVendorAdapter vendorAdapter() {
EclipseLinkJpaVendorAdapter vendorAdapter = new EclipseLinkJpaVendorAdapter();
vendorAdapter.setDatabase(Database.ORACLE);
vendorAdapter.setShowSql(true);
return vendorAdapter;
}
#Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
}
We have #ComponentScan set to check to bean in the package of com.zzz. In project B, we have a Struts2 Action that we want to inject a bean from project A. Here is a snippet of the class from project B:
#SuppressWarnings("rawtypes")
#Service("CommonSQLService")
public class CommonSQLServiceImpl implements CommonSQLService {
#Autowired
CommonSQLDao cdao;
#Override
public Policy getPolicyById(String policyNo) {
return cdao.getPolicyById(policyNo);
}
#Override
public Policy getPolicy(Policy parmRec) {
return cdao.getPolicy(parmRec);
}
#Override
public ArrayList<Policy> getPolicies(String policyNo) {
return cdao.getPolicies(policyNo);
}
#Override
public List<Policy> getPolicies(Policy parmRec){
return cdao.getPolicies(parmRec);
}
}
Here is our action (removed getters/setters):
package com.zzz.ma.mvc.action;
#SuppressWarnings("serial")
#Component
#Scope("prototype")
#Namespace("/Login")
public class LoginAction extends ActionParent {
#Autowired
private IUserService userService; //Contained in project B
#Autowired
private CommonSQLService commonSQLService; //What's autowired from project A
private User user;
private String updateFlag;
private Integer parentMenuId;
private IwifWebNavmenuItem record;
public static final String LOGIN_STR = "login";
#Action(value = "doLogin", results = { #Result(name = "success", location = "/pages/login.jsp") })
public String login() {
updateFlag = "Y";
return SUCCESS;
}
#Action(value = "validate", results = {
#Result(name = "success", location = "/BrowseOptions/showOptions", type = "redirect"),
#Result(name = "login", location = "/pages/login.jsp") })
public String validateUser() {
if (updateFlag == null) {
session.remove(LOGIN_STR);
return LOGIN;
}
if (userService.validateUser(user.getUsername(), user.getPassword()) == null) {
addActionError(getText("invalid.user"));
return LOGIN;
}
session.put(LOGIN_STR, user.getUsername());
return SUCCESS;
}
#Action(value = "logout", results = { #Result(name = "success", location = "/pages/login.jsp") })
public String logout() {
updateFlag = StringUtils.EMPTY;
session.invalidate();
return SUCCESS;
}
}
Whenever we try to do this autowire, we get an error like the one listed above. What are we missing? Can't classes contained in a jar file with Spring Stereotypes be autowired?
Thanks for you help in advance!

Change #Service("CommonSQLService") to #Service("commonSQLService") or you can just use #Service if you're not implementing the CommonSQLService interface anywhere else.

Related

How to do integration testing for custom mongo repository?

These are the following configuration that I have done in my project.
#Configuration
public class AppMongoConfig {
#Autowired private MongoDbFactory mongoDbFactory;
#Autowired private MongoMappingContext mongoMappingContext;
#Bean
public MappingMongoConverter mappingMongoConverter() {
DbRefResolver dbRefResolver = new DefaultDbRefResolver(mongoDbFactory);
MappingMongoConverter converter = new MappingMongoConverter(dbRefResolver, mongoMappingContext);
converter.setTypeMapper(new DefaultMongoTypeMapper(null));
return converter;
}
}
#Configuration
#RequiredArgsConstructor
#EnableConfigurationProperties(MultipleMongoProperties.class)
public class MultipleMongoConfig {
private final MultipleMongoProperties mongoProperties;
#Primary
#Bean(name = "primaryMongoTemplate")
public MongoTemplate primaryMongoTemplate() throws Exception {
return new MongoTemplate(primaryFactory(this.mongoProperties.getPrimary()));
}
#Bean(name = "secondaryMongoTemplate")
public MongoTemplate secondaryMongoTemplate() throws Exception {
return new MongoTemplate(secondaryFactory(this.mongoProperties.getSecondary()));
}
#Bean
#Primary
public MongoDbFactory primaryFactory(final MongoProperties mongo) throws Exception {
return new SimpleMongoDbFactory(new MongoClientURI(mongo.getUri()));
}
#Bean
public MongoDbFactory secondaryFactory(final MongoProperties mongo) throws Exception {
return new SimpleMongoDbFactory(new MongoClientURI(mongo.getUri()));
}
}
#Data
#ConfigurationProperties(prefix = "mongodb")
public class MultipleMongoProperties {
private MongoProperties primary = new MongoProperties();
private MongoProperties secondary = new MongoProperties();
}
#Configuration
#EnableMongoRepositories(
basePackages = {"com.student.repository.primary"},
mongoTemplateRef = "primaryMongoTemplate")
public class PrimaryMongoConfig {}
#Configuration
#EnableMongoRepositories(
basePackages = {"com.student.repository.secondary"},
mongoTemplateRef = "secondaryMongoTemplate")
public class SecondaryMongoConfig {}
Repository code:
public interface StudentDAO {
Student save(StudentInfo studentInfo);
}
#Repository
public class StudentDAOImpl implements StudentDAO {
#Autowired #Qualifier("primaryMongoTemplate")
private MongoTemplate mongoTemplate;
#Override public StudentInfo save(StudentInfo userWatchlist) {
return mongoTemplate.save(userWatchlist);
}
}
Integration Testing code:
#RunWith(SpringRunner.class)
#DataMongoTest(includeFilters = #Filter(Repository.class))
public class WatchListDAOImplIT {
#Autowired private StudentDAO studentDAO;
#Test
public void save() {
StudentInfo studentInfo = getStudentInfo();
StudentInfo dbUserWatchlist = watchListDAO.save(studentInfo);
Assert.assertEquals(studentInfo.getId(), dbUserWatchlist.getId());
}
private StudentInfo getStudentInfo() {
StudentInfo studentInfo = new StudentInfo();
studentInfo.setId(9999999l);
return studentInfo;
}
}
Which is giving me following error:-
java.lang.IllegalStateException: Failed to load ApplicationContext
at
org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125)
at
org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:108)
at
org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:
: org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'appMongoConfig': Unsatisfied dependency
expressed through field 'mongoDbFactory'; nested exception is
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'primaryFactory' defined in class path
resource [.../config/mongo/MultipleMongoConfig.class]: Unsatisfied
dependency expressed through method 'primaryFactory' parameter 0;
nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
qualifying bean of type
'org.springframework.boot.autoconfigure.mongo.MongoProperties'
available: expected at least 1 bean which qualifies as autowire
candidate. Dependency annotations: {}
Caused by:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
qualifying bean of type
'org.springframework.boot.autoconfigure.mongo.MongoProperties'
available: expected at least 1 bean which

Autowireing of Qualified Datasource not working

I have a SpringBoot 2.0.4 app, and I am using JdbcTemplates. I had it all working, when I got a requirement to do a data transfer between 2 DBs.
So I set up 2 Data Sources like this:
#Configuration
public class OracleConfiguration {
#Bean(name = "oracleDataSource")
#ConfigurationProperties(prefix = "oracle.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
}
and my DAO is like this:
#Repository
#Component
public class personDao extends JdbcDaoSupport {
static final Logger logger = LoggerFactory.getLogger(CymNetworkDao.class);
#Autowired
public void setDs(#Qualifier("oracleDataSource") DataSource dataSource) {
setDataSource(dataSource);
}
public List<PersonBean> findAll() {
List<PersonBean> result = getJdbcTemplate().query("SELECT * FROM PERSON", new PersonRowMapper());
return result;
}
}
I am getting this error:
Unsatisfied dependency expressed through field 'jdbcTemplate';
nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type 'org.springframework.jdbc.core.JdbcTemplate' available:
expected at least 1 bean which qualifies as autowire candidate. Dependency annotations:
{#org.springframework.beans.factory.annotation.Autowired(required=true), #org.springframework.beans.factory.annotation.Qualifier(value=oracleJdbcTemplate)}
My reading of tutorials was telling me to just autowire the datasource, and the jdbcTemplate would create itself. Am I qualifying wrong or something else?
you should create jdbcTemplate by injecting DataSource like following example
#Repository
public class personDao {
private JdbcTemplate oracleJdbcTemplate;
#Autowired
public void setDataSource(#Qualifier("oracleDataSource") DataSource dataSource) {
this.oracleJdbcTemplate = new JdbcTemplate(dataSource);
}
And in error message it clearly shows it was missing JdbcTemplate bean
#org.springframework.beans.factory.annotation.Qualifier(value=oracleJdbcTemplate)}

Spring and ServletContextListener

I read some articles and posts here about
#Autowired
ServletContext context;
Of my realization of ServletContextListener to #Controller. But I got a long exception when run it:
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'trendsoftCtrl':
Unsatisfied dependency expressed through field 'context':
No qualifying bean of type [javax.servlet.ServletContext] found for dependency [javax.servlet.ServletContext]:
expected at least 1 bean which qualifies as autowire candidate for this dependency.
Dependency annotations:
{#org.springframework.beans.factory.annotation.Autowired(required=true)};
nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type [javax.servlet.ServletContext] found for dependency [javax.servlet.ServletContext]:
expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations:
{#org.springframework.beans.factory.annotation.Autowired(required=true)}
Here are my classes.
#WebListener
public class TRSCListner implements ServletContextListener {
#Override
public void contextInitialized(ServletContextEvent event) {
ServletContext context = event.getServletContext();
String beanFileName = context.getInitParameter("springBeans");
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.load(beanFileName);
ctx.refresh();
NewsDao newsDao = ctx.getBean("newsDao", NewsDao.class);
context.setAttribute("appContext", ctx);
context.setAttribute("dao", newsDao);
}
#Override
public void contextDestroyed(ServletContextEvent event) {
ServletContext context = event.getServletContext();
GenericXmlApplicationContext ctx = (GenericXmlApplicationContext) context.getAttribute("appContext");
ctx.close();
}
}
And another
#Controller
public class TrendsoftCtrl {
#Autowired
ServletContext context;
#RequestMapping ("/welcome")
public ModelAndView helloWorld() {
NewsDao newsDao = (NewsDao) context.getAttribute("dao");
List<News> news = newsDao.getAll();
StringBuilder message = new StringBuilder();
for (News n : news) {
message.append(n.getCategory().getName() + "<br>");
message.append(n.getName() + "<br>");
message.append(n.getData() + "<br><br>");
}
return new ModelAndView("welcome", "news", message.toString());
}
public ServletContext getContext() {
return context;
}
public void setContext(ServletContext context) {
this.context = context;
}
}
By default, the #Autowired will perform the dependency checking to make sure the property has been wired properly. When Spring can’t find a matching bean to wire, it will throw an exception. To fix it, you can disable this checking feature by setting the “required” attribute of #Autowired to false.
#Autowired(required=false)
Even if Spring can’t find a matching bean, it will leave the person property unset.

NoSuchBeanDefinitionException on using Aspect

Spring boot version used: 1.2.3
We are creating CacheManager based on a property like :
#Bean
public CacheManager cacheManager() throws IOException {
String cacheImpl = env.getProperty(CacheSupport.CACHE_PROPERTY, String.class, "");
if (CacheSupport.AEROSPIKE.equalsIgnoreCase(cacheImpl)) {
return aerospikeCacheManager();
} else if (CacheSupport.REDIS.equalsIgnoreCase(cacheImpl)) {
return redisCacheManager();
} else {
return guavaCacheManager();
}
}
We have an AerospikeController like below :
#ConditionalOnProperty(name = CacheSupport.CACHE_PROPERTY, havingValue = CacheSupport.AEROSPIKE)
#Controller
public class AerospikeController {
#Autowired
private AerospikeCacheManager aerospikeCacheManager;
}
Things were working fine. Problem arises when i added below code for intercepting response times of aerospike calls:
#Component
#Aspect
public class WebServiceResponseTimeLogger {
private static final Logger LOG = LoggerFactory.getLogger(WebServiceResponseTimeLogger.class);
#Autowired
private AerospikeCacheManager cacheManager;
#PostConstruct
void init() {
LOG.info("WebServiceRequestTimeLogger initialized");
}
public WebServiceResponseTimeLogger() {
super();
}
#Pointcut("execution(* com.snapdeal.pie.web.aerospike.config.AerospikeCacheManager.*(..))")
public void pointcuts() {
}
#Around("pointcuts()")
public void logExternalCalls(ProceedingJoinPoint pjp) throws Throwable {
long timeStartInMillisecs = System.currentTimeMillis();
pjp.proceed();
MethodSignature signature = (MethodSignature) pjp.getSignature();
LOG.info("Time Taken to execute : {} {} is {} ms", new Object[] { signature, pjp.getArgs()[0], (System.currentTimeMillis() - timeStartInMillisecs) });
}
}
I am getting below exception :
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.snapdeal.pie.web.aerospike.config.AerospikeCacheManager] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1301)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1047)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
... 18 common frames omitted
From the logs it says :
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.snapdeal.pie.web.aerospike.config.AerospikeCacheManager] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
Check if bean AerospikeCacheManager is created in this code correctly.
#Bean
public CacheManager cacheManager() throws IOException {
String cacheImpl = env.getProperty(CacheSupport.CACHE_PROPERTY, String.class, "");
if (CacheSupport.AEROSPIKE.equalsIgnoreCase(cacheImpl)) {
return aerospikeCacheManager();

Hibernate - Spring MVC Error creating bean

I am new to Spring and I tried to make an application based on this tutorial: http://websystique.com/spring/spring4-hibernate4-mysql-maven-integration-example-using-annotations. I've seen some similar questions but I couldn't figure it out, I still get this error:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'RESTController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public service.UserService controller.RESTController.userService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [service.UserService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true), #org.springframework.beans.factory.annotation.Qualifier(value=userService)}
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: public service.UserService controller.RESTController.userService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [service.UserService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true), #org.springframework.beans.factory.annotation.Qualifier(value=userService)}
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [service.UserService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true), #org.springframework.beans.factory.annotation.Qualifier(value=userService)}
Here is the configuration class:
#Configuration
#EnableTransactionManagement
#ComponentScan({ "configuration" })
#PropertySource(value = { "classpath:database.properties" })
public class HibernateConfiguration {
#Autowired
private Environment environment;
#Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setPackagesToScan(new String[] { "model" });
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
#Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName"));
dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
dataSource.setUsername(environment.getRequiredProperty("jdbc.username"));
dataSource.setPassword(environment.getRequiredProperty("jdbc.password"));
return dataSource;
}
private Properties hibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect"));
properties.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql"));
properties.put("hibernate.format_sql", environment.getRequiredProperty("hibernate.format_sql"));
return properties;
}
#Bean
#Autowired
public HibernateTransactionManager transactionManager(SessionFactory s) {
HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(s);
return txManager;
}
}
The controller:
#RestController
public class RESTController {
#Autowired
#Qualifier("userService")
public UserService userService;
#RequestMapping(value = "/allGrades", method = RequestMethod.GET)
public ResponseEntity<List<Grade>> listAllGrades() {
List<Grade> grades = userService.getAllGrades();
if(grades.isEmpty()){
return new ResponseEntity<List<Grade>>(HttpStatus.NO_CONTENT);
}
return new ResponseEntity<List<Grade>>(grades, HttpStatus.OK);
}
This is the service:
#Service("userService")
#Transactional
public class UserServiceImp implements UserService{
private static final AtomicLong counterGrades = new AtomicLong();
private static final AtomicLong counterStudents = new AtomicLong();
private static List<Grade> grades;
private static List<Student> students;
#Autowired
private StudentDAO studentDAO;
#Autowired
private GradeDAO gradeDAO;
public UserServiceImp() {
}
public void saveGrade(Grade grade) {
gradeDAO.saveGrade(grade);
}
public List<Grade> getAllGrades() {
return gradeDAO.getAllGrades();
}
and the servlet-servlet.xml configuration file:
<context:annotation-config></context:annotation-config>
<context:component-scan
base-package="controller, dao, model, service, main, configuration">
</context:component-scan>
<mvc:annotation-driven></mvc:annotation-driven>
<mvc:default-servlet-handler />
<tx:annotation-driven />
Thanks a lot for help!
The #Controller requires the UserService to perform it's work. It is being #Autowired, but also with a #Qualifier("userService"). In this example I would not have used the #Qualifier because there are no other conflicting implementations of the UserService interface (just UserServiceImp). That said, the #Service has been defined with "userService" so the #Autowired with the #Qualifier should work. I can't explain why it is not for you, but suspect it has to do with how #Transactional is proxied.
In short, remove the #Qualifier("userService") line and #Autowired should work fine (Autowiring should be matched by UserService.
If that doesn't work, try moving the #Transactional annotation down from the class to the two methods in the class

Resources