1. 1.1 基于注解的IOC配置
  2. 既注解配置和xml配置要实现的功能都是一样的,都是要降低程序间的耦合.只是配置的形式不一样.
  3.  
  4. 1.2 环境搭建
  5. 1.2.1 第一步:拷贝必备的jar
  6. 需要多拷贝一个spring-aop-4.2.4.RELEASE.jar
  7. 1.2.2 创建xml文件,导入约束
  8. <?xml version="1.0" encoding="UTF-8"?>
  9. <!-- 导入schema
  10. 约束的位置在:
  11. ..\spring-framework-4.2.4.RELEASE\docs\spring-framework-reference\html\xsd-configuration.html
  12. 文件中。
  13. 注意:要导入schema约束
  14. -->
  15. <beans xmlns="http://www.springframework.org/schema/beans"
  16. xmlns:context="http://www.springframework.org/schema/context"
  17. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  18. xsi:schemaLocation="http://www.springframework.org/schema/beans
  19. http://www.springframework.org/schema/beans/spring-beans.xsd
  20. http://www.springframework.org/schema/context
  21. http://www.springframework.org/schema/context/spring-context.xsd ">
  22. </beans>
  23. 1.2.3 使用@Component注解配置管理的资源
  24. /**
  25. * 客户的业务层实现类
  26. * @author zhy
  27. *
  28. @Component(value="customerService")
  29. public class CustomerServiceImpl implements ICustomerService {
  30. @Override
  31. public void saveCustomer() {
  32. System.out.println("执行了保存客户");
  33. }
  34. }
  35. 1.2.4 第四步在spring的配置文件中开启spring对注解ioc的支持
  36. <!--告知spring框架在,读取配置文件,创建容器时,扫描注解,依据注解创建对象,并存入容器中.>
  37. <context:component-scan base-package="com.baidu"></context:component-sacn>
  38. 1.3 常用注解
  39. 1.3.1 用于创建对象的
  40. 相当于 : <bean id="" class="">
  41. 1.3.1.1 @Component
  42. 作用:
  43. 把资源让spring来管理.相当于在xml配置一个bean.
  44. 属性:
  45. value: 指定bean的id.如果不指定value属性,默认bean的id是当前类的类名.首字母小写.
  46. 1.31.2 @Controller @Service @Repository
  47. 们三个注解都是针对一个的衍生注解,他们的作用及属性都是一模一样的。
  48. 他们只不过是提供了更加明确的语义化。
  49. @Controller:一般用于表现层的注解。
  50. @Service:一般用于业务层的注解。
  51. @Repository:一般用于持久层的注解。
  52. 细节:如果注解中有且只有一个属性要赋值时,且名称是value,value在赋值是可以不写。
  53. 1.3.2 用于注入数据的
  54. 相当于 : <property name="" ref=""> 或者 <property name="" value="">
  55. 1.3.2.1@Autowired
  56. 作用:
  57. 自动按照类型注入。当使用注解注入属性时,set方法可以省略。它只能注入其他bean类型。当有多个类型匹配时,使用要注入的对象变量名称作为bean的id,在spring容器查找,找到了也可以注入成功。找不到就报错。
  58. /**
  59. * @Autowired 自动装配,自动按类型注入对象
  60. * <bean id="userService" class="com.baidu.demo2.UserServiceImpl" scope="" init-method="init">
  61. * <property name="userDao" ref="ud"/>
  62. * </bean>
  63. *
  64. * @Autowired
  65. @Qualifier(value="userDao") 这2个注解必须要一起使用,按id名称注入
  66.  
  67. @Resource 是Java提供注解,Spring容器支持该注解。可以通过name在容器中查找指定名称的对象
  68. *
  69. 1.3.2.2@Qualifier
  70. 作用:
  71. 在自动按照类型注入的基础之上,再按照Bean的id注入。它在给字段注入时不能独立使用,必须和@Autowire一起使用;但是给方法参数注入时,可以独立使用。
  72. 属性:
  73. value:指定bean的id。
  74. 1.3.2.3@Resource
  75. 作用:
  76. 直接按照Bean的id注入。它也只能注入其他bean类型。
  77. 属性:
  78. name:指定bean的id。
  79. 1.3.2.4@Value
  80. 作用:
  81. 注入基本数据类型和String类型数据的
  82. 属性:
  83. value:用于指定值
  84. 1.3.3用于改变作用范围的:
  85. 相当于:<bean id="" class="" scope="">
  86. 1.3.3.1@Scope
  87. 作用:
  88. 指定bean的作用范围。
  89. 属性:
  90. value:指定范围的值。
  91. 取值:singleton prototype request session globalsession
  92. 1.3.4和生命周期相关的:(了解)
  93. 相当于:<bean id="" class="" init-method="" destroy-method="" />
  94. 1.3.4.1@PostConstruct
  95. 作用:
  96. 用于指定初始化方法。
  97. 1.3.4.2@PreDestroy
  98. 作用:
  99. 用于指定销毁方法。
  100. 1.3.5代码示例
  101. 业务层代码:
  102. /**
  103. * 客户的业务层接口
  104. */
  105. public interface ICustomerService {
  106. /**
  107. * 保存客户
  108. * @param customer
  109. */
  110. void saveCustomer();
  111. }
  112.  
  113. /**
  114. * 客户的业务层实现类
  115. */
  116. //作用就相当于在xml中配置了一个bean标签,该注解有value属性,含义是bean的id。
  117. //不写的时候,默认的id是:当前类名,且首字母小写。即:customerServiceImpl
  118. @Component(value="customerService")
  119. @Scope(value="singleton")
  120. public class CustomerServiceImpl implements ICustomerService {
  121. // @Autowired
  122. // 自动按照数据类型注入,拿着当前变量的数据类型在spring的容器中找,找到后,给变量赋值。
  123. // 当有多个类型匹配时,会使用当前变量名称customerDao作为bean的id,继续在容器中找。
  124. // 找到了,也能注入成功。找不到就报错。
  125. // @Qualifier(value="customerDao2")//在自动按照类型注入的基础之上,再按照id注入
  126. @Resource(name="customerDao2")//直接按照bean的id注入
  127. private ICustomerDao customerDao = null;
  128.  
  129. @Value("com.mysql.jdbc.Driver")//注入基本类型和String类型数据
  130. private String driver;
  131.  
  132. @Override
  133. public void saveCustomer() {
  134. System.out.println(driver);
  135. customerDao.saveCustomer();
  136. }
  137. }
  138.  
  139. 持久层代码:
  140. /**
  141. * 客户的持久层接口
  142. */
  143. public interface ICustomerDao {
  144. /**
  145. * 保存客户
  146. */
  147. void saveCustomer();
  148. }
  149.  
  150. /**
  151. * 客户的持久层实现类11111111111111111111
  152. */
  153. @Repository("customerDao1")
  154. public class CustomerDaoImpl implements ICustomerDao {
  155.  
  156. @Override
  157. public void saveCustomer() {
  158. System.out.println("保存了客户111111111111111111");
  159. }
  160. }
  161.  
  162. /**
  163. * 客户的持久层实现类222222222222222222222222
  164. */
  165. @Repository("customerDao2")
  166. public class CustomerDaoImpl2 implements ICustomerDao {
  167.  
  168. @Override
  169. public void saveCustomer() {
  170. System.out.println("保存了客户2222222222222222222");
  171. }
  172. }
  173.  
  174. 测试类代码:
  175. public class Client {
  176. public static void main(String[] args) {
  177. //1.获取容器
  178. ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
  179. //2.根据id获取对象
  180. ICustomerService cs = (ICustomerService) ac.getBean("customerService"); cs.saveCustomer();
  181. }
  182. }
  183.  
  184. 配置文件:
  185. <?xml version="1.0" encoding="UTF-8"?>
  186. <!-- 我们导入约束时,除了昨天的那部分之外,还要单独导入一个context名称空间 -->
  187. <beans xmlns="http://www.springframework.org/schema/beans"
  188. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  189. xmlns:context="http://www.springframework.org/schema/context"
  190. xsi:schemaLocation="http://www.springframework.org/schema/beans
  191. http://www.springframework.org/schema/beans/spring-beans.xsd
  192. http://www.springframework.org/schema/context
  193. http://www.springframework.org/schema/context/spring-context.xsd">
  194. <!-- 告知spring框架在通过读取配置文件创建容器时,扫描的包,并根据包中类的注解创建对象-->
  195. <context:component-scan base-package="com.baidu"></context:component-scan>
  196. </beans>
  197.  
  198. 1.3.6 关于Spring注解和XML的选择问题
  199. 注解的优势 :
  200. 配置简单,维护方便(我们找到类,就相当于找到了对应的配置)
  201. XML的优势 :
  202. 修改时,不用改源码.不涉及重写编译和部署.
  203. Spring管理Bean方式的比较 :
  204. 基于XML配置 基于注解配置
  205. Bean定义 <bean id="..." class=".."> @Component 衍生类@Repository @Service @Controller
  206. Bean名称 通过idname指定 @Component("person")
  207. Bean注入 <property>或者通过p命名空间 @Autowired按类型注入 @Qualifier按名称注入
  208. 生命过程, init-method destroy-method @PostConstruct初始化 @PreDestroy销毁 @Scope设置作用范围
  209. Bean作用范围 范围scope属性
  210. 适合场景 Bean来自第三方,使用其它 Bean的实现类由用户自己开发
  211. 1.5 spring的纯注解配置
  212. /**
  213. * 客户的业务层实现类
  214. */
  215. @Configuration//表明当前类是一个配置类
  216. @ComponentScan(basePackages = "com.baidu")//配置要扫描的包
  217. public class SpringConfiguration {
  218. }
  219.  
  220. 那么新的问题又来了,我们如何获取容器呢?
  221. public class Client {
  222. public static void main(String[] args) {
  223. //1.获取容器:由于我们已经没有了xml文件,所以再用读取xml方式就不能用了。
  224. //这时需要指定加载哪个类上的注解
  225. ApplicationContext ac =
  226. new AnnotationConfigApplicationContext(SpringConfiguration.class);
  227. //2.根据id获取对象
  228. ICustomerService cs = (ICustomerService) ac.getBean("customerService");
  229. cs.saveCustomer();
  230. }
  231. }
  232. 1.5.3新注解说明
  233. 1.5.3.1@Configuration
  234. 作用:
  235. 用于指定当前类是一个spring配置类,当创建容器时会从该类上加载注解。获取容器时需要使用AnnotationApplicationContext(有@Configuration注解的类.class)。
  236. 属性:
  237. value:用于指定配置类的字节码
  238. 示例代码:
  239. /**
  240. * 用于初始化spring容器的配置类
  241. */
  242. @Configuration
  243. public class SpringConfiguration{
  244. }
  245. 1.5.3.2@ComponentScan
  246. 作用:
  247. 用于指定spring在初始化容器时要扫描的包。作用和在springxml配置文件中的:
  248. <context:component-scan base-package="com.baidu"/>是一样的。
  249. 属性:
  250. basePackages:用于指定要扫描的包。和该注解中的value属性作用一样。
  251.  
  252. 1.5.3.4@Import
  253. 作用:
  254. 用于导入其他配置类,在引入其他配置类时,可以不用再写@Configuration注解。当然,写上也没问题。
  255. 属性:
  256. value[]:用于指定其他配置类的字节码。
  257. 示例代码:
  258. @Configuration
  259. @ComponentScan(basePackages = "cn.baidu.spring")
  260. @Import({ Configuration_B.class})
  261. public class Configuration_A {
  262. }
  263.  
  264. @Configuration
  265. @PropertySource("classpath:info.properties")
  266. public class Configuration_B {
  267. }
  268. 1.5.3.5@Bean
  269. 作用:
  270. 该注解只能写在方法上,表明使用此方法创建一个对象,并且放入spring容器。它就相当于我们之前在xml配置中介绍的factory-beanfactory-method
  271. 属性:
  272. name:给当前@Bean注解方法创建的对象指定一个名称(即beanid)。
  273. 示例代码:
  274. @Bean(name = "datasource2")
  275. public DataSource createDS() throws Exception {
  276. ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
  277. comboPooledDataSource.setUser("root");
  278. comboPooledDataSource.setPassword("1234");
  279. comboPooledDataSource.setDriverClass("com.mysql.jdbc.Driver");
  280. comboPooledDataSource.setJdbcUrl("jdbc:mysql:///spring_ioc");
  281. return comboPooledDataSource;
  282. }
  283. 单元整合
  284. /**
  285. * Spring整合Junit单元测试
  286. * @author Administrator
  287. */
  288. @RunWith(value=SpringJUnit4ClassRunner.class)
  289. @ContextConfiguration(value="classpath:applicationContext.xml")
  290. public class Demo1 {
  291.  
  292. // 测试哪个对象,可以使用resource注解把对象注入进来
  293. @Resource(name="userService")
  294. private UserService userService;
  295.  
  296. @Resource(name="userDao")
  297. private UserDao userDao;
  298.  
  299. /**
  300. * Spring 整合Juint单元测试的方法
  301. */
  302. @Test
  303. public void run3(){
  304. userService.save();
  305. }
  306.  
  307. @Test
  308. public void run4(){
  309. // userService.update();
  310. userDao.save();
  311. }
  312. 使用XML方式完成IOC的入门
  313. 1. 导入jar
  314. 2. 编写接口和实现类
  315. 3. 编写配置文件,管理实现类
  316. 4. 编写入门的程序
  317. 使用注解的方式完成IOC的入门
  318. 1. 导入jar
  319. 2. 编写接口和实现类
  320. 3. 编写applicationContext.xml配置文件,目的 : 让注解生效
  321. 4. 在实现类上编写注解
  322. 5. 编写入门的程序
  323.  
  324. 1. 管理类的
  325. @Component 所有类都可以使用
  326. @Controller Web层使用的注解,就是Action使用的.
  327. @Service 业务层使用的注解
  328. @Repository 持久层使用的注解
  329. 2. 依赖注入的注解 (set方法可以省略不写)
  330. @Value 给普通类型属性注入值(String int double)
  331. @Resource 给引用类型注入值的,强调 : 该注解的属性时name
  332. @Scope 对象作用范围,默认单例的.
  333.  
  334. Spring整合WEB
  335. ServletContext对象,服务器启动就创建,服务器关闭就销毁.
  336. 监听器 : 监听ServletContext域对象的创建和销毁的.
  337. ServletContextListener监听器 , init destory
  338. 总结 :
  339. 服务器启动后,Spring创建对象,Spring创建Web版本的工厂,把工厂保存到ServletContext域对象中.
  340. 编写 :
  341. ServletContext对象中获取到Web版本的工厂,从工厂中获取到Service对象,调用方法.

SSH框架之Spring第二篇的更多相关文章

  1. SSH框架之Spring第一篇

    1.1. spring概述: 1.1.1 spring介绍 : Spring是分层的Java SE/EE应用 full-stack轻量级开源框架,以IoC(Inverse Of Control : 反 ...

  2. SSH框架之Struts2第二篇

    1.2 知识点 1.2.1 Struts2的Servlet的API的访问 1.2.1.1 方式一 : 通过ActionContext实现 页面: <h1>Servlet的API的访问方式一 ...

  3. SSH框架之Hibernate第二篇

    1.1 持久化类的编写规则 1.1.1 什么是持久化类? 持久化类 : 与表建立了映射关系的实体类,就可以称之为持久化类. 持久化类 = Java类 + 映射文件. 1.1.2 持久化类的编写规则 ( ...

  4. Maven环境下搭建SSH框架之Spring整合Hibernate

    © 版权声明:本文为博主原创文章,转载请注明出处 1.搭建环境 Spring:4.3.8.RELEASE Hibernate:5.1.7.Final MySQL:5.7.17 注意:其他版本在某些特性 ...

  5. SSH框架中spring的原理

    在ssh项目中,是有明确分工的,spring的作用就相当于将struts和hibernate连接起来,是将两个没有关系的框架的特性,方法,action都放在spring的配置文件中使他们建立关系.取他 ...

  6. Spring第二篇和第三篇的补充【JavaConfig配置、c名称空间、装载集合、JavaConfig与XML组合】

    前言 在写完Spring第二和第三篇后,去读了Spring In Action这本书-发现有知识点要补充,知识点跨越了第二和第三篇,因此专门再开一篇博文来写- 通过java代码配置bean 由于Spr ...

  7. Spring第二篇【Core模块之快速入门、bean创建细节、创建对象】

    前言 上篇Spring博文主要引出了为啥我们需要使用Spring框架,以及大致了解了Spring是分为六大模块的-.本博文主要讲解Spring的core模块! 搭建配置环境 引入jar包 本博文主要是 ...

  8. Eclipse搭建SSH框架(Struts2+Spring+Hibernate)

    见识少的我经过一天多的研究才知道,在MyEclipse中搭好的框架的配置文件和jar包是通用的.接下来——亮剑! 工具:Eclipse+Tomcat+Mysql 一.先在Eclipse中配置好Tomc ...

  9. SSH框架之Spring+Struts2+Hibernate整合篇

    回顾 -Hibernate框架 ORM: 对象关系映射.把数据库表和JavaBean通过映射的配置文件映射起来, 操作JavaBean对象,通过映射的配置文件生成SQL语句,自动执行.操作数据库. 1 ...

随机推荐

  1. 转:JDK1.8-Stream()使用详解

    为什么需要 Stream Stream 作为 Java 8 的一大亮点,它与 java.io 包里的 InputStream 和 OutputStream 是完全不同的概念.它也不同于 StAX 对 ...

  2. 洛谷 CF402A Nuts 题解

    本蒟蒻又来发题解啦! 这题是个紫题? 好吧,恶意评分可海星? 回到正题 这题很明显是贪心啊: 有a个坚果,b个隔板,x个隔板,最多分成v个区间. 那么我们的贪心策略是: 如果一共使用的挡板小于x,且当 ...

  3. .Net Core使用Ocelot网关(一) -负载,限流,熔断,Header转换

    1.什么是API网关 API网关是微服务架构中的唯一入口,它提供一个单独且统一的API入口用于访问内部一个或多个API.它可以具有身份验证,监控,负载均衡,缓存,请求分片与管理,静态响应处理等.API ...

  4. 使用 webservice 实现 RPC 调用

    WebService 介绍 Web service 是一个平台独立的,低耦合的 web 的应用程序用于开发分布式的互操作的应用程序.Web Service 技术, 能使得运行在不同机器上的不同应用无须 ...

  5. IPhone下json的解析 NSJSONSerialization

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式. 易于人阅读和编写.同时也易于机器解析和生成. 它基于JavaScript Programming Lan ...

  6. LightOJ1199 Partition Game

    Alice and Bob are playing a strange game. The rules of the game are: Initially there are n piles. A ...

  7. HYSBZ 1036树链剖分

    一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t : 把结点u的权值改为t II. QMAX u v: 询问从 ...

  8. 什么是RESTful?RESTfule风格

    导读 理解什么是REST之前,先去脑补以下什么是HTTP,参考[Http协议] 什么是REST? REST(英文:Representational State Transfer,简称REST,意思:表 ...

  9. Mac 配置 PlantUML

    PlantUML简介 UML: Unified Modeling Language 统一建模语言,是非专利的第三代建模和规约语言.UML是一种开放的方法,用于说明.可视化.构建和编写一个正在开发的.面 ...

  10. SSM 轻量级框架构建:图书管理系统

    一.接业务,作分析 1.大致业务要求 1.1 使用 SSM( Spring MVC + Spring + MyBatis )实现图书信息管理系统, MySQL5.5 作为后台数据库,该系统包括查询图书 ...