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());
}
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
bean
@Component
放在类上边,表示该类被Spring管理了,就是bean
属性如何注入
@Value("张三")
放在属性上边,表示该属性的默认值为“张三”
也可以放在set方法上
衍生的注解
@Component注解有几个衍生注解,在web开发中会按MVC三层架构分层
dao【@Repository】
service【@Service】
controller【@Controller】
以上四个注解效果都是一样的,都是讲类注册到Spring中,装配bean
自动装配(上边已经讲过)
@Autowired
@Qualifier
@Nullable
@Resource
作用域
@Scope("prototype")
选项参考xml配置文件方式singleton、prototype。。。
默认还是singleton
小结
XML于注解
XML更加智能,适用于任何场合,维护方便
注解不是自己的类用不了,维护相对复杂
XML于注解最佳实践
XML用来管理bean
注解只负责完成属性的注入
使用Java方式配置Spring
完全不用Spring的XML配置了,全权交给Java来做
JavaConfig是Spring的一个子项目,在Spring4之后成为了核心功能
获取容器方式
ApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
主要注解
@Configuration
相当于之前的beans.xml
@ComponentScan("com.baidu.config")
用法类似于beans.xml
@Import(MyConfig2.class)
合并其他config类
@Bean
相当于beans.xml中的bean,方法名相当于id,返回值相当于class
@Component
标记类被Spring托管
核心代码
@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");
}
代理模式
这个是SpringAOP的底层【SpringAOP和SpringMVC】
静态代理
角色
抽象角色,一般会使用接口或抽象类解决
真实角色,被代理角色
代理角色,代理真实角色,代理后一般会做一些附属操作
客户角色,访问代理对象的人
核心代码(房东租房案例)
// 抽象角色,接口
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 + "方法。。。");
}
}
动态代理
静态代理和动态代理的角色一样
动态代理的类是自动生成的,不是我们直接写好的
动态代理分为两大类
基于接口的:JDK【本案例使用】
基于类的:cglib
Java字节码:javassist(一个日本人发明的)
需要了解两个类
Proxy:代理
InvocationHandler:调用处理程序
主要逻辑
创建一个自动代理类来实现InvocationHandler
声明一个属性(被代理的接口)并增加set方法
声明一个返回代理类的方法
实现invoke方法,处理代理实例并返回结果
调用者
定义被代理对象
实例化自动代理工具类
注入被代理对象
生成代理对象
调用代理对象里的方法
核心代码
// 我的自动代理类
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();
}
}
public class MyPoint {
public void before() {
System.out.println("——————before被执行了——————");
}
public void after() {
System.out.println("——————after被执行了——————");
}
}
文章评论
还没有评论,欢迎留下第一条回复。
登录 或 注册 后发表评论。