spring mvc 注解:No bean named 'userBiz' is defined

J2EE 码拜 10年前 (2015-04-14) 1683次浏览 0个评论

之前发过一个帖子,但是因为我不知道论坛不能连续回复3次也不能删评论,弄的我不能发言了。
报的错是
严重: StandardWrapper.Throwable
org.springframework.beans.factory.BeanCreationException: Error creating bean with name “”userController””: Autowiring of fields failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.user.biz.UserBiz com.user.controller.UserController.userBiz; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.user.biz.UserBiz] is defined: Unsatisfied dependency of type [interface com.user.biz.UserBiz]: expected at least 1 matching bean
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessAfterInstantiation(AutowiredAnnotationBeanPostProcessor.java:243)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:959)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(Native Method)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:402)
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:316)
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:282)
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:126)
at javax.servlet.GenericServlet.init(GenericServlet.java:158)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1284)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1197)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1087)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5229)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5516)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1575)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1565)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.user.biz.UserBiz com.user.controller.UserController.userBiz; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.user.biz.UserBiz] is defined: Unsatisfied dependency of type [interface com.user.biz.UserBiz]: expected at least 1 matching bean
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:435)
at org.springframework.beans.factory.annotation.InjectionMetadata.injectFields(InjectionMetadata.java:105)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessAfterInstantiation(AutowiredAnnotationBeanPostProcessor.java:240)
… 30 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.user.biz.UserBiz] is defined: Unsatisfied dependency of type [interface com.user.biz.UserBiz]: expected at least 1 matching bean
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:613)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:412)
… 32 more

八月 28, 2014 2:02:43 下午 org.apache.catalina.core.StandardContext loadOnStartup
严重: Servlet /web threw load() exception
org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.user.biz.UserBiz] is defined: Unsatisfied dependency of type [interface com.user.biz.UserBiz]: expected at least 1 matching bean
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:613)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:412)
at org.springframework.beans.factory.annotation.InjectionMetadata.injectFields(InjectionMetadata.java:105)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessAfterInstantiation(AutowiredAnnotationBeanPostProcessor.java:240)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:959)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(Native Method)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:402)
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:316)
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:282)
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:126)
at javax.servlet.GenericServlet.init(GenericServlet.java:158)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1284)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1197)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1087)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5229)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5516)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1575)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1565)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)

下面是配置文件
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-2.5.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-2.5.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>

spring mvc 注解:No bean named 'userBiz' is defined
com.user.biz.UserBiz你不贴一下吗,看看你怎么定义的,它报错啊
spring mvc 注解:No bean named 'userBiz' is defined
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就不行了呢?要怎么改呢?

spring mvc 注解:No bean named 'userBiz' is defined
UserDaoImpl没有实行service注册啊,加上这个了没有@Component
spring mvc 注解:No bean named 'userBiz' is defined
错了,是UserBizImpl 加了没有
spring mvc 注解:No bean named 'userBiz' is defined
UserDaoImpl有没有注入
@Repository
@Transactional
spring mvc 注解:No bean named 'userBiz' is defined
引用 5 楼 w3226327 的回复:

UserDaoImpl有没有注入
@Repository
@Transactional

我在UserBizImpl类名上加了@Component ,在UserDaoImpl类名上加了@Repository
 @Transactional,还是报一样的错

spring mvc 注解:No bean named 'userBiz' is defined
User 怎么写的,有没有序列化
spring mvc 注解:No bean named 'userBiz' is defined
引用 7 楼 w3226327 的回复:

User 怎么写的,有没有序列化

@Entity
@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
}

spring mvc 注解:No bean named 'userBiz' is defined
40分
@Autowired
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;

spring mvc 注解:No bean named 'userBiz' is defined
引用 9 楼 w3226327 的回复:

@Autowired
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;

不行啊,还是一样

spring mvc 注解:No bean named 'userBiz' is defined
现在新改的UserBizImpl 、userController、UserDaoImpl 再贴下改了这么多,已经跟基础的配置差不多了
spring mvc 注解:No bean named 'userBiz' is defined
UserController
@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();
}
……}

spring mvc 注解:No bean named 'userBiz' is defined
引用 11 楼 w3226327 的回复:

现在新改的UserBizImpl 、userController、UserDaoImpl 再贴下改了这么多,已经跟基础的配置差不多了

在12楼

spring mvc 注解:No bean named 'userBiz' is defined
试一试这个:

<context:component-scan base-package="**"/>

扫描根和所有子包。
不过最好别这样干,还是缩小范围为好,否则项目大了,调试时项目启动时间会让人无法忍受。

spring mvc 注解:No bean named 'userBiz' is defined
public UserBiz getUserBiz() {
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试一下

spring mvc 注解:No bean named 'userBiz' is defined
引用 14 楼 zhaozengguang 的回复:

试一试这个:

<context:component-scan base-package="**"/>

扫描根和所有子包。
不过最好别这样干,还是缩小范围为好,否则项目大了,调试时项目启动时间会让人无法忍受。

还是不行…一样的错

spring mvc 注解:No bean named 'userBiz' is defined
引用 15 楼 w3226327 的回复:

public UserBiz getUserBiz() {
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试一下

不行

spring mvc 注解:No bean named 'userBiz' is defined
你怎么这么悲剧啊,估计是你配置文件的问题了,配置文件的配置找个好一点的教程抄下
spring mvc 注解:No bean named 'userBiz' is defined
http://wenku.baidu.com/link?url=KG4K6yekUQA1Rb7ebbSFxLRqAcVGLS4DCTXZOE_x9kpc1u6z5sZ2u-EA_gX96dtSXa-c1Gu6JkUESn2FhtF-phaXusXZCXi8Z8uTXRMVwwm

我看这个百度的可以试试,出去Junit部分应该可以

spring mvc 注解:No bean named 'userBiz' is defined
引用 18 楼 w3226327 的回复:

你怎么这么悲剧啊,估计是你配置文件的问题了,配置文件的配置找个好一点的教程抄下

我web.xml,springmvc-servlet.xml,hibernate.cfg.xml的位置没问题吧?我经常因为配置文件放错了地弄了老半天
spring mvc 注解:No bean named 'userBiz' is definedspring mvc 注解:No bean named 'userBiz' is defined

spring mvc 注解:No bean named 'userBiz' is defined
没感觉有啥错,你要还让我看,就只能看你web.xml和springmvc-servlet.xml了,感觉你还是重写这2个文件比较好
spring mvc 注解:No bean named 'userBiz' is defined
引用 21 楼 w3226327 的回复:

没感觉有啥错,你要还让我看,就只能看你web.xml和springmvc-servlet.xml了,感觉你还是重写这2个文件比较好

web.xml
<?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>
能看出有什么问题吗?

spring mvc 注解:No bean named 'userBiz' is defined
这2个文件没感觉啥问题啊,你怎么是2个项目,一个java项目和一个web项目,怎么配的,找个其他的dataSource配置文件抄下试试
spring mvc 注解:No bean named 'userBiz' is defined
引用 23 楼 w3226327 的回复:

这2个文件没感觉啥问题啊,你怎么是2个项目,一个java项目和一个web项目,怎么配的,找个其他的dataSource配置文件抄下试试

我是4个项目,web,service,dao,model一层一个…

spring mvc 注解:No bean named 'userBiz' is defined
<?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: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>

你把我某个项目的这个改下试试,我不确定行不行

spring mvc 注解:No bean named 'userBiz' is defined
引用 25 楼 w3226327 的回复:
<?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: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>

你把我某个项目的这个改下试试,我不确定行不行

改哪个配置?

spring mvc 注解:No bean named 'userBiz' is defined
引用 26 楼 wu5363 的回复:

改哪个配置?

看你的应该是那个hibernate.cfg.xml,你注意啊,我hibernate是4不是3

spring mvc 注解:No bean named 'userBiz' is defined
userBiz 没有注入,你就看一下有没有userBiz 实现类。 实现类的注解对吗             
spring mvc 注解:No bean named 'userBiz' is defined
spring mvc 注解:No bean named 'userBiz' is defined
首先声明的userbiz 有了注解@autowired自动注入 就不需要get,set的注入  删除先
 注意你定义出来的UserBIz 的名字叫userbiz 。这里面需要你在UserBizImpl 中用service注解  @service(“userbiz”)注意里面的名字要跟你在userController里面定义的名字一致 这样才能自动生成  dao的注入一样 不需要用了注解还用get set来注入

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明spring mvc 注解:No bean named 'userBiz' is defined
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!