简介
优点
扩展
构造器注入
<bean id="user" class="com.baidu.pojo.User">
<constructor-arg index="0" value="李四"/>
bean>
<bean id="user" class="com.baidu.pojo.User">
<constructor-arg type="java.lang.String" value="王五"/>
bean>
<bean id="user" class="com.baidu.pojo.User">
<constructor-arg name="name" value="阿达"/>
bean>
set注入(重点)
<bean id="user" class="com.baidu.pojo.User">
<property name="name" value="张三"/>
bean>
<bean id="student" class="com.baidu.pojo.Student">
<property name="name" value="张三"/>
<property name="address" ref="address"/>
<property name="books">
<array>
<value>红楼梦value>
<value>西游记value>
array>
property>
<property name="hobbies">
<list>
<value>听歌value>
<value>跑步value>
list>
property>
<property name="card">
<map>
<entry key="idCard" value="111111222222223333"/>
<entry key="bankCard" value="123456789"/>
map>
property>
<property name="games">
<set>
<value>LOLvalue>
<value>COCvalue>
<value>BOBvalue>
set>
property>
<property name="wife">
<null/>
property>
<property name="info">
<props>
<prop key="学号">1001prop>
<prop key="性别">0prop>
props>
property>
bean>
扩展注入,可以简化xml配置
<bean id="user" class="com.baidu.pojo.User" p:name="张三" p:age="20"/>
<bean id="user" class="com.baidu.pojo.User" c:name="李四" c:age="18"/>
注入外部bean(set注入)
<bean id="userDaoImpl" class="com.baidu.dao.UserDaoImpl"/>
<bean id="userDaoMysqlImpl" class="com.baidu.dao.UserDaoMysqlImpl"/>
<bean id="userServiceImpl" class="com.baidu.service.UserServiceImpl">
<property name="userDao" ref="userDaoMysqlImpl"/>
bean>
public class UserServiceImpl implements UserService{
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
}
注入内部bean也叫嵌套bean(set注入),以及级联赋值
<bean id="user" class="com.baidu.pojo.User">
<property name="name" value="张三"/>
<property name="city">
<bean id="city" class="com.baidu.pojo.City">
<property name="name" value="北京"/>
bean>
property>
bean>
<bean id="city" class="com.baidu.pojo.City">
<property name="name" value="北京"/>
bean>
<bean id="user" class="com.baidu.pojo.User">
<property name="name" value="张三"/>
<property name="city" ref="city"/>
<property name="city.name" value="上海"/>
bean>
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
User user = context.getBean("user", User.class);
System.out.println(user.getName());
System.out.println(user.getCity().getName());
}
提取list等类型属性注入(需要引入util约束,语法:
,了解即可,此处省略)
Bean的作用域:scope
<bean id="user" class="com.baidu.pojo.User" c:name="李四" c:age="18" scope="singleton"/>
在XML中显式装配
<bean id="user" class="com.baidu.pojo.User" autowire="byName">
<property name="name" value="张三"/>
bean>
<bean id="user" class="com.baidu.pojo.User" autowire="byType">
<property name="name" value="张三"/>
bean>
在Java中显式装配
<context:annotation-config/>
<bean id="dog" class="com.baidu.pojo.Dog"/>
<bean id="cat" class="com.baidu.pojo.Cat"/>
<bean id="user" class="com.baidu.pojo.User"/>
public class User {
@Nullable // 表示可以为空
private String name;
@Autowired // 表示开启自动装配
@Nullable // 表示可以为空
private Dog dog;
@Autowired(required = false) // 开启自动装配,并且可以为空
@Qualifier(value = "cat") // 指定bean-id
private Cat cat;
}
<context:component-scan base-package="com.baidu.pojo"/>
<context:annotation-config/>
@Component // 表示该类被Spring托管了,就是bean
@Scope("prototype") // 作用域,选项参考xml配置文件方式singleton、prototype。。。
public class User {
@Value("张三") // 表示默认值是张三
private String name;
}
隐式装配
xmlns:context="http://www.springframework.org/schema/context"
@Nullable // 表示可以为空
@Qualifier(value = "cat") // 指定bean-id
@Resource(name="user")
<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"
xmlns:aop="http://www.springframework.org/schema/aop"
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.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
">
<context:component-scan base-package="com.baidu.pojo"/>
<context:annotation-config/>
beans>
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
ApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
@Configuration // 相当于之前的beans.xml
@ComponentScan("com.baidu.config") // 用法类似于beans.xml
@Import(MyConfig2.class) // 合并其他config类
public class MyConfig {
@Bean // 相当于beans.xml中的bean,方法名相当于id,返回值相当于class
public User getUser(){
return new User();
}
}
@Component
public class User {
@Value("张三")
private String name;
}
@Test
public void test01(){
ApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
User user = (User) context.getBean("getUser");
}
// 抽象角色,接口
public interface Rent {
// 租房
void rent();
}
// 被代理类,房东身份
public class Host implements Rent {
public void rent() {
System.out.println("房东要出租房。。。");
}
}
// 代理角色,中介身份
public class Proxy implements Rent {
private Host host;
public Proxy() {}
public Proxy(Host host) {
this.host = host;
}
public Host getHost() {
return host;
}
public void setHost(Host host) {
this.host = host;
}
public void rent() {
seeHouse();
host.rent();
sign();
}
public void seeHouse(){
System.out.println("中介带看房。。。");
}
public void sign(){
System.out.println("中介签合同。。。");
}
}
// 客户角色
public class Client {
public static void main(String[] args) {
Proxy proxy = new Proxy(new Host());
proxy.rent();
}
}
public interface UserService {
void add();
void delete();
void update();
void query();
}
public class UserServiceImpl implements UserService {
public void add() {
System.out.println("增加了一个用户。。。");
}
public void delete() {
System.out.println("删除了一个用户。。。");
}
public void update() {
System.out.println("修改了一个用户。。。");
}
public void query() {
System.out.println("查询了一个用户。。。");
}
}
public class UserServiceProxy implements UserService {
private UserServiceImpl userService = new UserServiceImpl();
public UserServiceProxy() {}
public UserServiceProxy(UserServiceImpl userService) {
this.userService = userService;
}
public void add() {
log("add");
userService.add();
}
public void delete() {
log("delete");
userService.delete();
}
public void update() {
log("update");
userService.update();
}
public void query() {
log("query");
userService.query();
}
public void log(String action) {
System.out.println("[debug]调用了" + action + "方法。。。");
}
}
// 我的自动代理类
public class MyProxyInvocationHandler implements InvocationHandler {
// 被代理的接口
private Object target;
public void setTarget(Object target) {
this.target = target;
}
// 代理类
public Object getProxy() {
return Proxy.newProxyInstance(this.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
}
// 处理代理实例并返回结果
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
log(method.getName());
return method.invoke(target, args);
}
public void log(String action) {
System.out.println("执行了" + action + "方法");
}
}
// 调用者
public class Client {
public static void main(String[] args) {
// 被代理对象
Host host = new Host();
// 自动代理工具类
MyProxyInvocationHandler handler = new MyProxyInvocationHandler();
// 注入被代理对象
handler.setTarget(host);
// 生成代理对象
Rent proxy = (Rent) handler.getProxy();
// 调用代理对象里的方法
proxy.rent();
}
}
<dependency>
<groupId>org.aspectjgroupId>
<artifactId>aspectjweaverartifactId>
<version>1.9.7version>
dependency>
<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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
">
public class MyAfterLog implements AfterAdvice {
// returnValue:返回值
// method:要执行的目标对象的方法
// args:参数
// target:目标对象
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("【后置log】" + target.getClass().getName() + "的" + method.getName() + "被执行了。。。返回结果为:" + returnValue);
}
}
<bean id="userService" class="com.baidu.service.UserServiceImpl"/>
<bean id="beforeLog" class="com.baidu.log.MyBeforeLog"/>
<bean id="afterLog" class="com.baidu.log.MyAfterLog"/>
<aop:config>
<aop:pointcut id="pointcut" expression="execution(* com.baidu.service.UserServiceImpl.*(..))"/>
<aop:advisor advice-ref="beforeLog" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
aop:config>
public class MyPoint {
public void before() {
System.out.println("——————before被执行了——————");
}
public void after() {
System.out.println("——————after被执行了——————");
}
}
<bean id="userService" class="com.baidu.service.UserServiceImpl"/>
<bean id="myPoint" class="com.baidu.diy.MyPoint"/>
<aop:config>
<aop:aspect ref="myPoint">
<aop:pointcut id="point" expression="execution(* com.baidu.service.UserServiceImpl.*(..))"/>
<aop:before method="before" pointcut-ref="point"/>
<aop:after method="after" pointcut-ref="point"/>
aop:aspect>
aop:config>
@Component // 注册bean
@Aspect // 注册切面
public class MyAnnotationPoint {
// 定义切入点
@Before("execution(* com.baidu.service.UserServiceImpl.*(..))")
public void before() {
System.out.println("——————before——————");
}
}
<bean id="userService" class="com.baidu.service.UserServiceImpl"/>
<context:annotation-config/>
<context:component-scan base-package="com.baidu"/>
<aop:aspectj-autoproxy/>