Spring注解注入
来源:互联网 发布:字符串大小写转换c语言 编辑:程序博客网 时间:2024/06/14 16:04
/*****************************************************************************************************/
9:18 2017/10/19 注解注入
/*****************************************************************************************************/
·对于Spring框架,类中的空构造一定要写,不管注入方式是什么。
·通过构造器注入,在配置文件中,使用的是<constructor-arg></constructor-arg>标签。
·注入的三种方式:
(1)、构造器注入。(需要一个带参的构造)
(2)、自动注入。
(3)、set方法注入。(比较常用)
范例:
<!-- bean标签中 autowire="byName"表示的是自动注入;使用最多的是set方法的注入,使用的是property标签-->
<bean id = "Action" class = "com.wk.action.Action" scope = "prototype" autowire="byName">
<!-- 通过构造器注入 ,基本不用-->
<!-- <constructor-arg ref="userService"></constructor-arg> -->
<!--<property name="userService" ref = "userService"></property>-->
</bean>
·当感觉映射文件态麻烦时,引入了注解。
·使用注解的话,先要加上相关的约束。
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"
·打开注解:
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config/>
</beans>
·创建对象用@Component注解。
依赖注入的形式:@Resource。
·注解默认的形式是单例。
要设置成多例,是要在类上加@Scope("prototype")//注解默认的形式表示单例。设置成多例是prototype。
9:18 2017/10/19 注解注入
/*****************************************************************************************************/
·对于Spring框架,类中的空构造一定要写,不管注入方式是什么。
·通过构造器注入,在配置文件中,使用的是<constructor-arg></constructor-arg>标签。
·注入的三种方式:
(1)、构造器注入。(需要一个带参的构造)
(2)、自动注入。
(3)、set方法注入。(比较常用)
范例:
<!-- bean标签中 autowire="byName"表示的是自动注入;使用最多的是set方法的注入,使用的是property标签-->
<bean id = "Action" class = "com.wk.action.Action" scope = "prototype" autowire="byName">
<!-- 通过构造器注入 ,基本不用-->
<!-- <constructor-arg ref="userService"></constructor-arg> -->
<!--<property name="userService" ref = "userService"></property>-->
</bean>
·当感觉映射文件态麻烦时,引入了注解。
·使用注解的话,先要加上相关的约束。
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"
·打开注解:
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config/>
</beans>
·创建对象用@Component注解。
依赖注入的形式:@Resource。
·注解默认的形式是单例。
要设置成多例,是要在类上加@Scope("prototype")//注解默认的形式表示单例。设置成多例是prototype。
阅读全文
0 0
- 【Spring】Spring注解注入
- 【Spring】Spring注解注入
- Spring依赖注入:注解注入
- spring注解注入
- Spring注解注入
- spring注解注入
- spring注解注入
- Spring注解注入
- Spring注解注入
- spring的注解注入
- Spring注解注入
- 【Spring】注解注入bean
- Spring注解注入
- Spring注解注入
- spring 注解实现注入
- Spring注解注入
- Spring注解注入
- Spring注解注入
- springboot 中ResourceBundle 加载配置文件 报java.util.MissingResourceException
- 单词倒置问题的答案和注释(C语言)
- 第879期机器学习日报(2017-02-13)
- 数据科学家自我修养——一份数据科学的开放课程清单
- Java实现FTP与SFTP文件上传
- Spring注解注入
- 第5周项目3——建立链栈算法库
- 玩消费电子产品,常用6款传感器不可不知
- 【其他】无法正常打开网页
- 二维码的生成原理
- 第881期机器学习日报(2017-02-15)
- 第883期机器学习日报(2017-02-17)
- 第884期机器学习日报(2017-02-18)
- 第882期机器学习日报(2017-02-16)