2、依赖注入
来源:互联网 发布:过山车大亨3 mac 中文 编辑:程序博客网 时间:2024/06/05 06:30
一、为什么用spring
以前上厕所没带纸,只能自己撅起屁股出去拿,这样做会有麻烦比如屎可能会粘在裤子上,滴在地板上,不清洁。
用了spring中IOC(DI)之后,就可以打电话叫马仔帮你把厕纸拿过来。这样就能保证清洁,安安静静的做一个美男子。
IOC 我打电话叫马仔拿厕纸。主语是我。
DI 马仔知道我没厕纸就去拿了。主语是马仔。
OOP 整个铜锣湾都知道老大上厕所不带纸,于是就在每个厕所都装了厕纸箱
AOP 后来马仔们发现,就算全部装了厕纸箱,老大有时候还是会因为各种原因没有厕纸用,于是干脆就把厕纸箱全撤了,然后安排一个人,专门给老大送厕纸。
二、初步使用 DI(依赖注入) IOC容器
1、新建一个普通Java工程
2、先导入spring必要的包:
commons-loggingspring-beansspring-contextspring-corespring-expression
3、目录机构:
--com.oursnail --controller --UserController.jav --entity --Student.java --Teacher.java --resources --application.xml --service --UserService.java --UserServiceImpl.java --Main.java
4、读取简单的Bean
Student.java:
public class Student { private int id; private String name; public Student(){} public Student(int id, String name) { this.id = id; this.name = name; } get()、set()...}
将这个bean交给spring管理
都是基于反射,这里是利用构造器进行赋值
<bean id="student" class="com.oursnail.entity.Student"> <constructor-arg name="id" value="11"/> <constructor-arg name="name" value="helloworld"/> </bean>
这是对成员属性进行赋值
<bean id="student" class="com.oursnail.entity.Student"> <property name="id" value="2"/> <property name="name" value="hhhh"/> </bean>
这里完整的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" xsi:schemaLocation="http://www.springframework.org/schema/beans"> <!--<bean id="student" class="com.oursnail.entity.Student"> <constructor-arg name="id" value="11"/> <constructor-arg name="name" value="helloworld"/> </bean>--> <bean id="student" class="com.oursnail.entity.Student"> <property name="id" value="2"/> <property name="name" value="hhhh"/> </bean></beans>
Main.java:
public class Main { public static void main(String[] args) { //加载spring配置 ApplicationContext context = new ClassPathXmlApplicationContext("./com/oursnail/resources/application.xml"); //spring利用反射获取对象 Student student = context.getBean(Student.class); //读属性值 System.out.println(student.getId()); System.out.println(student.getName()); }}
5、读取稍微复杂点的对象
//配置文件 //注入数组 <property name="arry"> <list> <value>1</value> <value>2</value> </list> </property> //注入list <property name="list"> <list> <value>3</value> <value>4</value> </list> </property> //注入map <property name="map"> <map> <entry key="name" value="melon"></entry> <entry key="passowrd" value="123"></entry> </map> </property>
本实例中新建一个Teacher.java:
public class Teacher { private int id; private List<String> classNames; private Student student; get()、set()...}
application.xml中:
<bean id="teacher" class="com.oursnail.entity.Teacher"> <property name="id" value="22"/> <property name="classNames"> <list> <value>111</value> <value>222</value> <value>333</value> <value>444</value> <value>555</value> </list> </property> <property name="student" ref="student"/></bean>
完整的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" xsi:schemaLocation="http://www.springframework.org/schema/beans"> <!--<bean id="student" class="com.oursnail.entity.Student"> <constructor-arg name="id" value="11"/> <constructor-arg name="name" value="helloworld"/> </bean>--> <bean id="student" class="com.oursnail.entity.Student"> <property name="id" value="2"/> <property name="name" value="hhhh"/> </bean> <bean id="teacher" class="com.oursnail.entity.Teacher"> <property name="id" value="22"/> <property name="classNames"> <list> <value>111</value> <value>222</value> <value>333</value> <value>444</value> <value>555</value> </list> </property> <property name="student" ref="student"/> </bean></beans>
这样实现了一些复杂对象的设值。
总结:我们通过上面的实验,可以感受到spring如何管理bean的,即spring解析xml文件,通过反射获取对象及其值。
6、注解
常用的注解有:
@Controller:根据用户请求控制视图@Service:业务逻辑层@Component:不明确的层,又需要注入时可以用这个注解
在service层下面创建UserService接口:
public interface UserService { void say();}
继而写实现类:
@Servicepublic class UserServiceImpl implements UserService{ @Override public void say() { System.out.println("hello spring"); }}
在controller层下面写UserController,注入userService:
@Controllerpublic class UserController { @Autowired private UserService userService; public UserService getUserService() { return userService; }}
在application.xml中扫描注解包:
先引入jar:spring-aop、aopalliance、aspectjweaver
要想扫描包,需要引入schema约束:
xmlns:context="http://www.springframework.org/schema/context"http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsd
扫描包的位置:
<context:component-scan base-package="com.oursnail.**"/>
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:context="http://www.springframework.org/schema/context" 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-4.0.xsd"> <!--扫描com.oursnail下面的所有层数的文件--> <context:component-scan base-package="com.oursnail.**"/></beans>
最后启动Main,输出hello spring则成功:
public class Main { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("./com/oursnail/resources/application.xml"); UserController userController = context.getBean(UserController.class); UserService userService = userController.getUserService(); userService.say(); }}
完整代码下载,spring基础1-DI
- 依赖注入2
- Spring依赖注入---<2>
- 31-依赖注入-2
- 2、依赖注入
- 依赖注入那些事儿【2】 之 探究依赖注入
- spring依赖注入(2)-注解
- Angular 2中的依赖注入
- Angular 2中的依赖注入
- Spring依赖注入(2)
- 使用Dagger 2 依赖注入
- 依赖注入
- 依赖注入
- 依赖注入
- 依赖注入
- 依赖注入
- 依赖注入
- 依赖注入
- 依赖注入
- 关于IDea的内存设置
- 方差、标准差、均方差、均方误差区别总结
- 调节当前页面的屏幕亮度
- [DevExpress使用随笔]之NavBarControl控件(一)
- 微软扩大Windows服务器对Linux容器的支持
- 2、依赖注入
- awk
- java实现图片压缩(格式、裁剪)--------
- ubuntu找回root密码,报错authentication token manipulation error
- spring连接数据库一直连不上
- char*,const char*和string的相互转换
- define与 inline的区别
- linux 查看系统版本 32位 or 64位
- MSP430G2553 SPI UART