我在AutoWired注释中遇到了一个问题,我是春天的新手,请仔细阅读以下代码并帮助我解决此代码与JSF和Hibenate的完美结合,现在我正在整合Spring框架工作。和我使用的依赖关系请指导我是否需要对UserDetailsVO和UserDetailsHelper类使用componentsnet注释我也使用了服务和Repository注释, 我已经在managedbean setter方法中制作了Qualifier和AutoWired(required = true),但仍然没有用。
弹簧ORM
在弹簧上下文加载调度程序servlet.xml文件时加载通用Crud操作的抽象Dao类
public abstract class AbstractDao<T, ID extends Serializable> implements Dao<T, ID> {
private final Class<T> persistentClass;
// private SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
@Autowired
private SessionFactory sessionFactory;
@SuppressWarnings("unchecked")
public AbstractDao(){
this.persistentClass =(Class<T>) ((ParameterizedType)this.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}
protected Session getSession(){
return this.sessionFactory.getCurrentSession();
}
public Class<T> getPersistentClass(){
return persistentClass;
}
@Override
public T findById(ID id) {
this.getSession().getTransaction().begin();
return (T) this.getSession().load(this.getPersistentClass(), id);
}
}
通用接口的Dao接口我还需要注释接口(我不这么认为)
public interface Dao<T, ID> {
public List<T> findAll();
public T findById(ID id);
}
User DetailsDao接口特定类接口
public interface UserDetailsDao extends Dao<UserDetails, String> {
}
The UserDetailsDao Impl and its Implemetation
@Repository("userDetailsDaoImpl")
public class UserDetailsDaoImpl extends AbstractDao<UserDetails, String>
implements UserDetailsDao {
}
UserDetailsService接口服务接口
public interface UserDetailsService extends UserDetailsDao {
}
用户详细信息Dao Service Impl及其实现Spring Service类,如标准所示,并将接口注释为自动连线是否正确?
@Service("userDetailsServiceImpl")
@Transactional
public class UserDetailsServiceImpl implements UserDetailsService {
@Autowired
UserDetailsDao userDetailsDao;
@Override
public List<UserDetails> findAll() {
return userDetailsDao.findAll();
}
@Override
public UserDetails findById(String id) {
return userDetailsDao.findById(id);
}
}
UserDetailsVo用于Do to Vo转换的Pojo类这个简单用于DO对象之间的简单转换是用hibernate注释复制的
@Component("userVo")
public class UserDetailsVo implements Serializable {
private String userId;
private String userName;
private String emailId;
private boolean status;
private String userLevel;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
//
}
The Helper Class
@Service("userHelper") // the helper class to used for business logics and some convertions please help me with below code style i'm following is it correct as i'm passing new object when converting to VO to DO vice versa
public class UserDetailsHelpler实现Serializable {
private static final Logger logger = Logger.getLogger(UserDetailsHelpler.class);
/**
*
*/
private static final long serialVersionUID = 1L;
private UserDetailsServiceImpl usersImpl;
@Autowired
public void setUsersImpl(UserDetailsServiceImpl usersImpl) {
this.usersImpl = usersImpl;
}
public UserDetailsVo saveUserDetails(UserDetailsVo vo) {
return userDetailsDOTOVOConvertor(usersImpl.save(userDetailsVOTODOConvertor(new UserDetails(), vo, vo.getUserId())), new UserDetailsVo());
}
}
Managed Bean类JSF ManagedBean类,JSF2.2只有注释,在faces配置中我也添加了弹簧视图解析器。
@ManagedBean(name="userDetailsManagedBean")
@SessionScoped
public class UserDetailsMangedBean implements Serializable {
private static final long serialVersionUID = 1L;
private static final Logger logger = Logger.getLogger(UserDetailsMangedBean.class);
private UserDetailsVo users;
private UserDetailsHelpler helpler;
private String loadValidValues;
private boolean readOnce;
private List<UserDetailsVo> usersList;
public UserDetailsMangedBean() {
logger.info("Inside the UserdetailsManagedBean:" );
if(null == users) users = new UserDetailsVo();
}
public String getLoadValidValues() {
if(!readOnce) {
logger.info("Helper value " + helpler);
usersList = helpler.readUserDetails();
readOnce = true;
}
return loadValidValues;
}
public void setLoadValidValues(String loadValidValues) {
this.loadValidValues = loadValidValues;
}
public UserDetailsVo getUsers() {
return users;
}
@Autowired
public void setUsers(UserDetailsVo users) {
this.users = users;
}
public UserDetailsHelpler getHelpler() {
return helpler;
}
@Autowired
public void setHelpler(UserDetailsHelpler helpler) {
this.helpler = helpler;
}
public List<UserDetailsVo> getUsersList() {
return usersList;
}
public void setUsersList(List<UserDetailsVo> usersList) {
this.usersList = usersList;
}
}
Stack跟踪清楚地表明它未能将HelperClass创建为bean而UserDetails服务Impl存在依赖性问题,这是我迷路的地方我尝试了一些更改,如autowired(required = true) 限定符(&#34; userHelper&#34) 将服务注释添加到UserDetailsHelper和UserDetailsVo等但仍然存在同样的问题。
INFO 2017-04-24 23:21:54,748 3194 org.springframework.beans.factory.config.PropertyPlaceholderConfigurer [localhost-startStop-1] Loading properties file from ServletContext resource [/WEB-INF/configfiles/jdbc.properties]
INFO 2017-04-24 23:21:56,184 4630 org.hibernate.validator.internal.util.Version [localhost-startStop-1] HV000001: Hibernate Validator 5.3.0.Alpha1
INFO 2017-04-24 23:21:57,018 5464 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter [localhost-startStop-1] Looking for @ControllerAdvice: Root WebApplicationContext: startup date [Mon Apr 24 23:21:52 IST 2017]; root of context hierarchy
INFO 2017-04-24 23:21:57,266 5712 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter [localhost-startStop-1] Looking for @ControllerAdvice: Root WebApplicationContext: startup date [Mon Apr 24 23:21:52 IST 2017]; root of context hierarchy
INFO 2017-04-24 23:21:58,066 6512 org.hibernate.Version [localhost-startStop-1] HHH000412: Hibernate Core {5.1.0.Final}
INFO 2017-04-24 23:21:58,071 6517 org.hibernate.cfg.Environment [localhost-startStop-1] HHH000206: hibernate.properties not found
INFO 2017-04-24 23:21:58,075 6521 org.hibernate.cfg.Environment [localhost-startStop-1] HHH000021: Bytecode provider name : javassist
WARN 2017-04-24 23:21:58,171 6617 org.hibernate.orm.deprecation [localhost-startStop-1] HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-configuration. Use namespace http://www.hibernate.org/dtd/hibernate-configuration instead. Support for obsolete DTD/XSD namespaces may be removed at any time.
INFO 2017-04-24 23:21:58,770 7216 org.hibernate.annotations.common.Version [localhost-startStop-1] HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
INFO 2017-04-24 23:22:00,174 8620 org.hibernate.dialect.Dialect [localhost-startStop-1] HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
INFO 2017-04-24 23:22:12,157 20603 org.hibernate.hql.internal.QueryTranslatorFactoryInitiator [localhost-startStop-1] HHH000397: Using ASTQueryTranslatorFactory
WARN 2017-04-24 23:22:12,747 21193 org.springframework.web.context.support.XmlWebApplicationContext [localhost-startStop-1] Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userHelper': Unsatisfied dependency expressed through method 'setUsersImpl' parameter 0: No qualifying bean of type [com.sounds.bvs.data.dao.serviceImpl.UserDetailsServiceImpl] found for dependency [com.sounds.bvs.data.dao.serviceImpl.UserDetailsServiceImpl]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.sounds.bvs.data.dao.serviceImpl.UserDetailsServiceImpl] found for dependency [com.sounds.bvs.data.dao.serviceImpl.UserDetailsServiceImpl]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
ERROR 2017-04-24 23:22:12,771 21217 org.springframework.web.context.ContextLoader [localhost-startStop-1] Context initialization failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userHelper': Unsatisfied dependency expressed through method 'setUsersImpl' parameter 0: No qualifying bean of type [com.sounds.bvs.data.dao.serviceImpl.UserDetailsServiceImpl] found for dependency [com.sounds.bvs.data.dao.serviceImpl.UserDetailsServiceImpl]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.sounds.bvs.data.dao.serviceImpl.UserDetailsServiceImpl] found for dependency [com.sounds.bvs.data.dao.serviceImpl.UserDetailsServiceImpl]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:647)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:349)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:775)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4853)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5314)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1407)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1397)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.sounds.bvs.data.dao.serviceImpl.UserDetailsServiceImpl] found for dependency [com.sounds.bvs.data.dao.serviceImpl.UserDetailsServiceImpl]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1403)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1056)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1018)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:639)
... 24 more `