web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/springmvc-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app> hibernate.cfg.xml <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Database connection settings --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/usertest</property> <property name="connection.username">root</property> <property name="connection.password">123456</property> <property name="hibernate.connection.url"> <![CDATA[jdbc:mysql://localhost:3306/usertest?useUnicode=true&characterEncoding=utf8]]> </property> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- Enable Hibernate's automatic session context management --> <property name="current_session_context_class">thread</property> <!-- Disable the second-level cache --> <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <property name="javax.persistence.validation.mode">none</property> <!-- Drop and re-create the database schema on startup --> <!--update也可以用create/create-drop/update/validate代替, create 表示可以根据实体配置文件来自动生成表(只能生成表) --> <property name="hbm2ddl.auto">update</property> <mapping class="com.user.model.User"/> </session-factory> </hibernate-configuration>下面是代码 UserController @Controller public class UserController { @Autowired private UserBiz userBiz; public UserBiz getUserBiz() { return userBiz; } @Autowired public void setUserBiz(UserBiz userBiz) { this.userBiz = userBiz; } ......} UserBiz public interface UserBiz { public void add(User u); public boolean login(String username, String password); public List<User> search(String name, String age); public void update(User u); public User getUser(String id); public void del(String id);} UserBizImpl public class UserBizImpl implements UserBiz{ @Resource private UserDao userDao; public UserDao getUserDao() { return userDao; } @Autowired public void setUserDao(UserDao userDao) { this.userDao = userDao; } .....} UserDao public interface UserDao {
public void add(User u) ; public boolean login(String name, String pwd) ; public List<User> search(String name, String age); public void update(User user); public User getUser(String id) ; public void del(String id);}UserDaoImpl public class UserDaoImpl implements UserDao { public static SessionFactory _SessionFactory; static { _SessionFactory = new AnnotationConfiguration().configure().buildSessionFactory(); } .....}说是没有userBiz这个bean,但是我不明白,userDao是用同样的方法注入的就可以,为什么biz就不行了呢?要怎么改呢?
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/usertest</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<property name="hibernate.connection.url">
<![CDATA[jdbc:mysql://localhost:3306/usertest?useUnicode=true&characterEncoding=utf8]]>
</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<property name="javax.persistence.validation.mode">none</property>
<!-- Drop and re-create the database schema on startup -->
<!--update也可以用create/create-drop/update/validate代替, create 表示可以根据实体配置文件来自动生成表(只能生成表) -->
<property name="hbm2ddl.auto">update</property>
<mapping class="com.user.model.User"/>
</session-factory>
</hibernate-configuration>下面是代码
UserController
@Controller
public class UserController {
@Autowired
private UserBiz userBiz; public UserBiz getUserBiz() {
return userBiz;
}
@Autowired
public void setUserBiz(UserBiz userBiz) {
this.userBiz = userBiz;
}
......}
UserBiz
public interface UserBiz { public void add(User u);
public boolean login(String username, String password);
public List<User> search(String name, String age);
public void update(User u);
public User getUser(String id);
public void del(String id);}
UserBizImpl
public class UserBizImpl implements UserBiz{
@Resource
private UserDao userDao; public UserDao getUserDao() {
return userDao;
}
@Autowired
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
.....}
UserDao
public interface UserDao {
public void add(User u) ;
public boolean login(String name, String pwd) ;
public List<User> search(String name, String age);
public void update(User user);
public User getUser(String id) ;
public void del(String id);}UserDaoImpl
public class UserDaoImpl implements UserDao { public static SessionFactory _SessionFactory;
static {
_SessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
}
.....}说是没有userBiz这个bean,但是我不明白,userDao是用同样的方法注入的就可以,为什么biz就不行了呢?要怎么改呢?
UserDaoImpl没有实行service注册啊,加上这个了没有@Component
@Repository
@Transactional
@Transactional,还是报一样的错
@Table(name = "usertest")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="id")
private int id;
@Column(name="name")
private String name;
@Column(name="pwd")
private String pwd;
@Column(name="age")
private int age; public User() {
}
//get和set
}
private UserBiz userBiz;
改为
@Resource
private UserBiz userBiz;
///////////////////////////////
@Resource
private UserDao userDao; public UserDao getUserDao() {
return userDao;
}
@Autowired
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
改为
@Autowired
private UserDao userDao;
@Controller
public class UserController {
@Resource
private UserBiz userBiz; public UserBiz getUserBiz() {
return userBiz;
}
@Autowired
public void setUserBiz(UserBiz userBiz) {
this.userBiz = userBiz;
}
......}
UserBizImpl@Component
public class UserBizImpl implements UserBiz{
@Autowired
private UserDao userDao;
public UserDao getUserDao() {
return userDao;
} public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
.....}
UserDaoImpl
@Repository
@Transactional
public class UserDaoImpl implements UserDao { public static SessionFactory _SessionFactory;
static {
// _SessionFactory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
_SessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
}
......}
<context:component-scan base-package="**"/>
扫描根和所有子包。
不过最好别这样干,还是缩小范围为好,否则项目大了,调试时项目启动时间会让人无法忍受。
return userBiz;
}
@Autowired
public void setUserBiz(UserBiz userBiz) {
this.userBiz = userBiz;
}
和这个public UserDao getUserDao() {
return userDao;
}public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
已经可以删掉了
将@Component 改为@Service试一下
我看这个百度的可以试试,出去Junit部分应该可以
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>springmvc-servlet.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:p="http://www.springframework.org/schema/p"
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/aop http://www.springframework.org/schema/aop/spring-aop-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-3.0.xsd">
<context:annotation-config />
<!-- 对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->
<context:component-scan base-package="*"/>
<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<!--对模型视图名称的解析,即在模型视图名称添加前后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:suffix=".jsp"/>
</beans>
能看出有什么问题吗?
<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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- 配置数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
<!-- <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>-->
<!-- <property name="url" value="jdbc:mysql://localhost:3306/mts"></property>-->
<!-- <property name="username" value="${jdbc.username}"></property>-->
<!-- <property name="password" value="123"></property>-->
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- 配置SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="javax.persistence.validation.mode">none</prop> <!--是做验证的-->
</props>
</property>
<!-- <property name="annotatedClasses">-->
<!-- <list>-->
<!-- <value>com.tgb.usermanager.model.User</value>-->
<!-- </list>-->
<!-- </property>-->
<property name="packagesToScan">
<list>
<value>com.tgb</value><!-- 扫描实体类,也就是平时所说的model -->
</list>
</property>
</bean>
<!-- 配置一个事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- <bean id="entityDao" class="com.tgb.common.dao.EntityDaoImpl"> -->
<!-- <property name="sessionFactory" ref="sessionFactory" />-->
<!-- </bean>-->
<!-- 支持 @AspectJ 标记
proxy-target-class属性,默认为false,表示使用jdk动态代理织入增强,
当配为<aop:aspectj-autoproxy poxy-target-class="true"/>时,表示使用CGLib动态代理技术织入增强。
不过即使proxy-target-class设置为false,如果目标类没有声明接口,则spring将自动使用CGLib动态代理。-->
<aop:aspectj-autoproxy />
<!-- 配置事务,使用代理的方式 -->
<!-- <bean id="transactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true"> -->
<!-- <property name="transactionManager" ref="transactionManager"></property> -->
<!-- <property name="transactionAttributes"> -->
<!-- <props> -->
<!-- <prop key="add*">PROPAGATION_REQUIRED,-Exception</prop> -->
<!-- <prop key="modify*">PROPAGATION_REQUIRED,-myException</prop> -->
<!-- <prop key="del*">PROPAGATION_REQUIRED</prop> -->
<!-- <prop key="*">PROPAGATION_REQUIRED</prop> -->
<!-- </props> -->
<!-- </property> -->
<!-- </bean> -->
<!-- 申明annotation 加载事务驱动 -->
<!-- <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>-->
<!-- 配置事务的传播特性 即事务的应用规则,使用tx标签配置的拦截器 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="create*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="merge*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="put*" propagation="REQUIRED" />
<tx:method name="use*" propagation="REQUIRED"/> <!--hibernate4必须配置为开启事务 否则 getCurrentSession()获取不到-->
<tx:method name="get*" propagation="REQUIRED" read-only="true" />
<tx:method name="count*" propagation="REQUIRED" read-only="true" />
<tx:method name="find*" propagation="REQUIRED" read-only="true" />
<tx:method name="list*" propagation="REQUIRED" read-only="true" /> <tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
<tx:annotation-driven/>
<!-- xml风格支持 -->
<aop:config expose-proxy="true">
<!-- 只对业务逻辑层实施事务 -->
<aop:pointcut id="txPointcut" expression="execution( * com.tgb.usermanager.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>
<!-- 配置事务,使用代理的方式 -->
<!-- <bean id="transactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true"> -->
<!-- <property name="transactionManager" ref="transactionManager"></property> -->
<!-- <property name="transactionAttributes"> -->
<!-- <props> -->
<!-- <prop key="add*">PROPAGATION_REQUIRED,-Exception</prop> -->
<!-- <prop key="modify*">PROPAGATION_REQUIRED,-myException</prop> -->
<!-- <prop key="del*">PROPAGATION_REQUIRED</prop> -->
<!-- <prop key="*">PROPAGATION_REQUIRED</prop> -->
<!-- </props> -->
<!---->
<!-- </property> -->
<!-- -->
<!-- </bean> -->
<!-- JDBC 用于自动注入 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg ref="dataSource" />
</bean>
<!-- JDBC 用于自动注入 -->
<bean id="simpleJdbcTemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
<constructor-arg ref="jdbcTemplate" />
</bean>
</beans>
你把我某个项目的这个改下试试,我不确定行不行
首先声明的userbiz 有了注解@autowired自动注入 就不需要get,set的注入 删除先
注意你定义出来的UserBIz 的名字叫userbiz 。这里面需要你在UserBizImpl 中用service注解 @service(“userbiz”)注意里面的名字要跟你在userController里面定义的名字一致 这样才能自动生成 dao的注入一样 不需要用了注解还用get set来注入