web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd"><display-name></display-name><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list><servlet><servlet-name>spring</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/classes/applicationContext.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>spring</servlet-name><url-pattern>*.do</url-pattern></servlet-mapping>
</web-app>

applicationContext.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:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx" 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/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd       http://www.springframework.org/schema/context    http://www.springframework.org/schema/context/spring-context-2.5.xsd"><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver"></property><property name="url" value="jdbc:mysql://127.0.0.1:3306/pecope"></property><property name="username" value="root"></property><property name="password" value="root"></property></bean><bean id="sessionFactory"class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"><property name="dataSource"><ref bean="dataSource" /></property><property name="hibernateProperties"><props><prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop></props></property><property name="annotatedClasses"><list><value>com.zbb.entity.Salesman</value><value>com.zbb.entity.Plan</value><value>com.zbb.entity.Schedule</value><value>com.zbb.entity.Room</value><value>com.zbb.entity.Customer</value><value>com.zbb.entity.Message</value><value>com.zbb.entity.Material</value><value>com.zbb.entity.Project</value><value>com.zbb.entity.Bargain</value><value>com.zbb.entity.Plan2</value></list></property></bean><context:component-scan base-package="com.zbb" use-default-filters="false"><context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/> <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan></beans>

BaseAction.java

package com.zbb.action;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;import com.zbb.entity.Customer;
import com.zbb.iservice.IBaseService;@Controller
@RequestMapping(value="/baseAction")
public class BaseAcion {@AutowiredIBaseService iBaseService;@RequestMapping(value="/getAll")public String getAll() {System.out.println("123");iBaseService.getAll(Customer.class);return "/success.jsp";}}

IBaseDao.java

若结合hibernatedaosupport查询数据库则要加上红色标记的部分不然出现异常,而且要注意懒加载问题

@Autowired  
    public void setMySessionFactory(SessionFactory sessionFactory){  
        super.setSessionFactory(sessionFactory);  
    }

package com.zbb.idao;import java.sql.SQLException;
import java.util.List;import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
@Repository
public class IBaseDao extends HibernateDaoSupport{public List<Object> getAll(final Class cls) {// TODO Auto-generated method stubSystem.out.println("basedao");return super.getHibernateTemplate().executeFind(new HibernateCallback() {public Object doInHibernate(Session arg0)throws HibernateException, SQLException {// TODO Auto-generated method stubreturn arg0.createQuery("from "+cls.getSimpleName()).list();}});}
@Autowired  public void setMySessionFactory(SessionFactory sessionFactory){  super.setSessionFactory(sessionFactory);  }
}

IBaseService.java

package com.zbb.iservice;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import com.zbb.idao.IBaseDao;
@Service
public class IBaseService{@AutowiredIBaseDao iBaseDao;public List<Object> getAll(Class cls) {// TODO Auto-generated method stubSystem.out.println("baseservice");iBaseDao.getAll(cls);return null;}}

Test.java

package com.zbb.iservice;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import com.zbb.idao.IBaseDao;
@Service
public class IBaseService{@AutowiredIBaseDao iBaseDao;public List<Object> getAll(Class cls) {// TODO Auto-generated method stubSystem.out.println("baseservice");iBaseDao.getAll(cls);return null;}}

转载于:https://www.cnblogs.com/super-admin/p/5515654.html

spring自动注入bean相关推荐

  1. 【Spring】Spring 自动注入(autowire)详解

    1.概述 转载:添加链接描述 2. 手动注入的不足 [Spring]Spring 依赖注入之手动注入 上篇文章中介绍了依赖注入中的手动注入,所谓手动注入是指在xml中采用硬编码的方式来配置注入的对象, ...

  2. Spring自动注入

    谈及一个问题,无非牵扯到三点,是什么,怎么来的,怎么用的 Spring自动注入是什么 是指容器中的一个组件中需要用到另一个组件(例如聚合关系)时,依靠spring容器创建对象,而不是手动创建: Spr ...

  3. 巧用 Spring 自动注入实现策略模式升级版

    一.前言 1.1 背景 在工作过程中,有时候需要根据不同的枚举(常量)执行不同的逻辑. 比如不同的用户类型,使用不同的优惠政策:不同的配置变化,走不同的处理逻辑等. 下面模拟一个根据不同用户类型,走不 ...

  4. Spring自动注入原理

    我的博客 spring的属性注入属于spring bean的生命周期一部分,bean的生命周期首先记住两个概念: spring bean:最终存在spring容器当中的对象 对象:实例化出来的对象,但 ...

  5. 基于spring自动注入及AOP的表单二次提交验证

    2019独角兽企业重金招聘Python工程师标准>>> 这几天在网上闲逛,看到了几个关于spring的token二次提交问题,受到不少启发,于是自己动手根据自己公司的项目框架结构,制 ...

  6. 【Spring学习】spring注解自动注入bean

    Spring mvc注解用到的配置: <!-- 启用spring mvc 注解 --> <context:annotation-config /> <context:co ...

  7. 解决Spring+Quartz无法自动注入bean问题

    问题 我们有时需要执行一些定时任务(如数据批处理),比较常用的技术框架有Spring + Quartz中.无奈此方式有个问题:Spring Bean无法自动注入. 环境:Spring3.2.2 + Q ...

  8. Spring自动注入(引用类型)

    spring可以根据某些规则给引用类型完成赋值,只对引用类型有效.有两种方式实现自动注入,下面简单的介绍以下 1-ByName 按名称自动注入:Java类中引用类型的属性名称和spring容器中bea ...

  9. Spring依赖注入Bean为空,注入失效场景

    场景介绍 使用spring往一个bean(BeanB)注入另一个bean(BeanA),发现BeanA为null,注入失败了. 代码展示 /*** @author huangd* @date 2021 ...

  10. Spring自动装配Bean

    除了使用 XML 和 Annotation 的方式装配 Bean 以外,还有一种常用的装配方式--自动装配.自动装配就是指 Spring 容器可以自动装配(autowire)相互协作的 Bean 之间 ...

最新文章

  1. 由于Eclipse版本不符造成的异常
  2. 使用正确的主机名访问https,提示连接不安全
  3. React组件生命周期-正确执行运行阶段的函数
  4. python自学教程-Python学习教程(一)自学资源分享
  5. 目标检测(四)--ICF
  6. DIY开源mini桌面i3结构3D打印机--分析
  7. 幼儿园数学目标_幼儿园数学活动目标的制定
  8. 6、halcon+串口通讯测试
  9. 宠物商家必须给猫狗植入芯片,6月1日生效
  10. 米扑代理:爬虫代理IP哪家好
  11. apkanalyzer.bat could NOT be found in D:\Program Files\android-sdk的解决方案
  12. 软件开发的非功能性需求
  13. 偏向锁-批量重偏向和批量撤销测试
  14. AGV导航中的最短路径算法比较
  15. 20210726 java基础复习
  16. 20款Adobe AIR小游戏
  17. 皇帝成长计划html文件打不开,皇帝成长计划源代码修改(共6篇).doc
  18. Matlab数学建模(八):评价型模型
  19. nginx配置日志记录问题
  20. 网络封包抓取工具 Winpcap

热门文章

  1. rocketmq 两个线程同时消费一个消息
  2. 数据库优化-水平拆分 垂直拆分
  3. xshell 输入w 命令后报错 66 column window is too narrow
  4. zabbix安装详解
  5. Java运算符和类型转换
  6. RabbitMQ 实现RPC
  7. Spring概述(精)
  8. BZOJ2002[HNOI2010] 弹飞绵羊(分块)
  9. 电商项目的app学习笔记(三)-嵌套路由组件的实现
  10. c语言json配置文件linux,Linux C 编程——使用json-c库读取json格式的配置文件