这是在我的applicationContext.xml文件中创建的bean。
<bean id="dataSource"
<class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/employeelist" />
<property name="username" value="root" />
<property name="password" value= xxxxxx />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>com.aspire.model.Employee</value>
</list>
</property>
</bean>
<bean id ="registrationService" class = "com.aspire.service.RegisterServiceImpl">
<property name="registerDAO" ref="registerDAO"></property>
<!-- <property name ="sessionFactory" ref = "sessionFactory"></property> -->
</bean>
<bean id="registerDAO" class ="com.aspire.dao.RegisterDAOImpl">
<property name ="sessionFactory" ref = "sessionFactory"></property>
</bean>
</beans>
这是RegisterService接口及其实现类文件。
public interface RegisterService {
public void checkRegister(String name,String email,Date dob,String address,String office,String serviceLine,String username,String passwd) throws SQLException,IOException, IllegalStateException, SystemException;
}
@Service("registrationService")
public class RegisterServiceImpl implements RegisterService{
@Autowired
private RegisterDAO register;
public RegisterServiceImpl(){
}
public RegisterServiceImpl(RegisterDAO register){
this.register = register;
}
public void checkRegister(String name, String email, Date dob, String address, String office, String serviceLine,
String username, String passwd) throws SQLException, IOException, IllegalStateException, SystemException {
System.out.println("In Service class...Check Register");
register.checkRegister(name, email, dob, address, office, serviceLine, username, passwd);
}
}
这是RegisterDAOImpl.java文件。
@Repository("registerDAO")
public class RegisterDAOImpl implements RegisterDAO{
@Resource(name="sessionFactory")
protected SessionFactory sessionFactory;
protected Session session;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public SessionFactory getSessionFactory(){
return sessionFactory;
}
protected Session getSession(){
return sessionFactory.openSession();
}
public void checkRegister(String name, String email, Date dob, String address, String office, String serviceLine,
String username, String passwd) throws SQLException,IOException, IllegalStateException, SystemException
{
System.out.println("In Check Register");
Integer emp_id = null;
try
{
session = getSession();
/*String SQL_QUERY = "INSERT INTO employee(emp_id,name,dob,address,office,sline,username,password,email)"+
"s";
Query query = session.createQuery(SQL_QUERY);
int result = query.executeUpdate();*/
org.hibernate.Transaction tx1=session.beginTransaction();
Employee emp = new Employee(name,dob,address,office,serviceLine,username,passwd,email);
emp.setEmp_id(111);
emp.setAddress("Chennai");
emp.setDob(dob);
emp.setEmail(email);
emp.setEmp_name("emp_name");
emp.setEmp_pwd("emp_pwd");
emp.setOffice_loc("office_loc");
emp.setServiceLine(serviceLine);
emp.setUsername("username");
emp_id = (Integer)session.save(emp);
System.out.println(emp_id);
tx1.commit();
}catch(HibernateException e)
{
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
finally
{
session.close();
}
}
}
这是我在创建bean时收到的错误消息。 引起:org.springframework.beans.NotWritablePropertyException:bean类的无效属性'registerDAO'[com.aspire.service.RegisterServiceImpl]:Bean属性'registerDAO'不可写或具有无效的setter方法。 setter的参数类型是否与getter的返回类型匹配?
提前谢谢你。很难解决这个问题。
答案 0 :(得分:0)
<bean id ="registrationService" class = "com.aspire.service.RegisterServiceImpl">
<property name="registerDAO" ref="registerDAO"></property>
<!-- <property name ="sessionFactory" ref = "sessionFactory"></property> -->
</bean>
这是setter依赖注入,你必须在类RegisterServiceImpl
中有一个setter方法。这就是错误所说的。
在服务类中添加如下方法:
public void setRegisterDAO(RegisterDAO registerDAO) {
this.registerDAO = registerDAO;
}
正如Deinum所说,你已经有了注释,所以你可以删除registrationService
&amp;的xml配置bean。 registerDAO
并使用component-scan
,如下所示:
<context:component-scan base-package="your-package" />
请参阅this example以了解如何操作。