Java 注释在spring中不工作
我是spring的初学者,我在spring中有一个dao类。但是注释@auto-wired或@service不起作用,我通过在应用程序上下文中创建一个bean解决了这个问题,注释在spring中不起作用的原因是什么。提供了上下文:component scan base package=但注释也不起作用Java 注释在spring中不工作,java,spring,Java,Spring,我是spring的初学者,我在spring中有一个dao类。但是注释@auto-wired或@service不起作用,我通过在应用程序上下文中创建一个bean解决了这个问题,注释在spring中不起作用的原因是什么。提供了上下文:component scan base package=但注释也不起作用 StudentDao public interface StudentDao { public int addStudent(StudentEntity s);
StudentDao
public interface StudentDao {
public int addStudent(StudentEntity s);
}
-----------------------------------
@Service("studentDaoImpl")
public class StudentDaoImpl implements StudentDao{
@PersistenceContext
private EntityManager em;
@Override
@Transactional
public int addStudent(StudentEntity student) {
// TODO Auto-generated method stub
em.persist(student);
return student.getId();
}
}
------------------------------------------------
FascadeDaoImpl
public class FascadeControllerImpl implements FascadeController {
// @Autowired
private StudentDao studentDao;
private UserContext uc;
public void studentDao() {
studentDao=(StudentDao) uc.getContext().getBean("studentDao");
}
}
public int addStudent(StudentEntity student) {
studentDao();
return studentDao.addStudent(student);
}
应用程序上下文
<?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:p="http://www.springframework.org/schema/p"
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/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-4.0.xsd">
<context:component-scan base-package="sms.spring.dao" />
<context:component-scan base-package="sms.spring.fascade" />
<context:component-scan base-package="sms.spring.entity" />
<context:annotation-config />
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
<bean class="sms.spring.entity.StudentEntity" id="studentbean"/>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.microsoft.sqlserver.jdbc.SQLServerDriver</value>
</property>
<property name="url">
<value>jdbc:sqlserver://localhost;databaseName=dbstsms</value>
</property>
<property name="username">
<value>sa</value>
</property>
<property name="password">
<value>sa123</value>
</property>
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
<property name="persistenceUnitName" value="PERSUnit" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServer2008Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="fascadeController" class="sms.spring.fascade.FascadeControllerImpl"></bean>
<bean id="studentDao" class="sms.spring.dao.StudentDaoImpl"></bean>
<bean id="loginDao" class="sms.spring.dao.LoginDaoImpl"></bean>
<bean id="facultyDao" class="sms.spring.dao.FacultyDaoImpl"></bean>
<bean id="markDao" class="sms.spring.dao.MarkDaoImpl"></bean>
<bean id="notificationDao" class="sms.spring.dao.NotificationDaoImpl"></bean>
<tx:annotation-driven />
问题不清楚,但看起来您的控制器也应该用@controller注释。所有属于Spring项目且具有自动关联依赖项的类都需要由Spring创建。简而言之,您永远不应该创建一个新的Spring类,并使用ApplicationContextgetBean或当它被另一个类注入时创建它们 此外,请记住,在创建bean时,构造函数的自连线依赖项为null,未初始化,您需要创建一个用@PostConstruct注释的init方法。两个主要原因是: 您必须告诉spring,在哪里扫描组件,确保包是正确的 您没有实际bean的实现,请确保StudentDao也有@Service
请发布您得到的错误,同样从您发布的代码中我可以看到,即使您使用@service annotation注释了StudentDaoImpl类,您仍然在xml配置中定义相同的错误,请使用基于注释的配置或基于xml的配置 请检查您的文件是否有错误 如果启用了组件扫描,则不需要xml配置文件中提到的 检查是否为包启用了组件扫描 有关正确的架构位置,请参阅答案
“不工作”不是我们可以帮助解决的问题的有效描述。您正在尝试将基于xml的配置与注释混合使用。这也是可能的,但是你需要照顾好事情。很难猜测代码的问题,因为您没有共享所有配置。我建议只使用基于xml的配置或基于注释的配置。org.springframework.beans.factory.BeanCreationException:创建名为“fascadebean”的bean时出错:自动连线依赖项的注入失败;嵌套异常为org.springframework.beans.factory.BeanCreationException:无法自动关联字段:private sms.spring.dao.StudentDaoImpl sms.spring.fascade.FascadeControllerImpl.StudentDaoImpl;嵌套异常为org.springframework.beans.factory.nosuchbeandefinitionExceptionion:找不到[sms.spring.dao.StudentDaoImpl]类型的符合依赖项条件的bean:应至少有1个bean符合此依赖项的autowire候选项的条件。依赖项批注:{org.springframework.beans.factory.annotation.Autowiredrequired=true@optimus刚刚意识到您正在搜索名为studentDao的bean,但在基于注释的配置中,您将bean名称指定为studentDaoImpl,因为基于注释的配置覆盖了基于xml的配置,您可能遇到了这个问题,您是否可以将studentDao=studentDao uc.getContext.getBeanstudentDao;更改为'studentDao=StudentDao uc.getContext.getBeanstudentDaoImpl;`仍然错误相同如果我在xml配置中指定它,我可以访问bean,这是使我的注释工作的任何可能的方法@Optimus丢失。这可能是注释配置不工作的原因,也可能是如果您正在使用注释配置,请从xml中删除它