Spring(IOC自动装配-基于注解开发)
Spring IoC 自动装载 autowire:
自动装载是Spring提供的一种更加简单的方式,来完成DI,不需要手动配置property ,IoC容器会自动选择Bean玩成注入。
自动装载俩种:
byName ,通过属性名完成自动装载
byType,通过属性对应的数据类型完成自动装载
byName
1.创建实体类
package com.southwind.entity;import lombok.Data; @Data
public class Presson {
private Integer Id;
private String name;
private Car car; }
2.在spring.xml中配置Car和Person的Bean,通过自动装载完成依赖注入
<bean id="person" class="com.southwind.entity.Presson" autowire="byName">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
</bean>
<bean id="car" class="com.southwind.entity.Car">
<constructor-arg name="num" value="1"> </constructor-arg>
<constructor-arg name="brand" value="奥迪"></constructor-arg>
</bean>
测试:
package com.southwind.test;
import com.southwind.entity.Presson;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test7 {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-autowire.xml");
Presson preson =(Presson)applicationContext.getBean("person");
System.out.println(preson);
}
}
byType:
1.实体类
2.在spring.xml中配置Car和Person的Bean,通过自动装载完成依赖注入
<bean id="person" class="com.southwind.entity.Presson" autowire="byType">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
</bean>
<bean id="car" class="com.southwind.entity.Car">
<constructor-arg name="num" value="1"> </constructor-arg>
<constructor-arg name="brand" value="奥迪"></constructor-arg>
</bean>
注意:
使用byType进行自动装载时,多个会有异常。
Spring IoC基于注解的开发
SpingIoC的作用帮助开发者创建项目中所需要的Bean,同时完成Bean之间的依赖注入关系。DI
实现功能有两种方式:
- 基于xml
- 基于注解
基于注解的配置有两部:配置自动扫包
<context:component-scan base-package="com.southwind.entity"></context:component-scan>添加注解: @Component (默认首字母类第一个字母小写为名字)
package com.southwind.entity;import lombok.Data;
import org.springframework.stereotype.Component; @Data
@Component
public class Repository {
private DataSouse dataSouse;
}
改名字:@Component(value = "myrepo")
package com.southwind.entity;import lombok.Data;
import org.springframework.stereotype.Component; @Data
@Component(value = "myrepo")
public class Repository {
private DataSouse dataSouse;
}
分析注入得到类:
String[] s= applicationContext.getBeanDefinitionNames();
for(String name ){
System.out.println(name);
}
DI:
package com.southwind.entity;
import lombok.Data;
import org.springframework.stereotype.Component;
@Data
@Component
public class DataSouse {
private String user;
private String password;
private String url;
private String driverName;
}
自动装载:@Autowired(默认时byType)
package com.southwind.entity;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Data
@Component
public class Repository {
@Autowired
private DataSouse dataSouse;
}
要改变为byName()配合 @Qualifier(value="DS")
@Data
@Component
public class Repository {
@Autowired
@Qualifier(value="DS")
private DataSouse dataSouse;
}
实体类中的普通的成员变量可以通过@Value进行赋值
@Data
@Component(value = "DS")
public class DataSouse {
@Value("root")
private String user;
@Value("root")
private String password;
@Value("jdbc:mysql://localhost:3306/library")
private String url;
@Value("com.mysql.cj.jdbc.Driver")
private String driverName;
}
最新文章
- IDEA tomcat乱码
- String字符串去掉最后一个";,";号的几种方式
- 20145304 Java第四周学习报告
- 使用javap反编译class文件
- SQL 比较时间大小
- C语言计算开方
- SoapUI中Groovy的实用方法
- extjs tablepanel 高度自适应有关问题
- html 中head显示 在标题栏里面的图片
- 常见LINQ语句学习
- ssh下常用操作汇总(good)
- ubuntu 安装Matlab 解决显示中文乱码
- 汽车Vin码识别——可以嵌入到手机里的新OCR识别技术
- oh-my-zsh配置
- class DELPHICLASS TObject
- http协议下载文件
- 强化学习---A3C
- c++ 类前置声明【转】
- BZOJ.3105.[CQOI2013]新Nim游戏(线性基 贪心 博弈论)
- ZOJ3770Ranking System 2017-04-14 12:42 52人阅读 评论(0) 收藏