IOC控制反转
创建实体类
不再new对象 用对象名.setXXX去给对象赋值 改用配置文件给对象赋值
<?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:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="取个名字" class="写实体类的全限定名"> <property name="实体类的属性名" value="要给属性赋的值"> <constructor-arg index="实体类属性名的下标" value="赋的值"/> </bean> </beans>
测试
public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("xml的名字是什么这里就写什么"); Hello hello = (Hello) context.getBean("xml文件里的bean的id是什么名字这里就写什么名字,注意大小写"); System.out.println(hello.toString()); }
Proxy代理模式
代理模式: 真实对象 代理对象 真实对象和代理对象共有的方法 测试 (三个类一个接口)
以租房为例
房东和中介共有的方法 将房屋出租出去
共有的方法:出租房屋
房东和中介都有这个方法就把这个方法写进接口里面,让房东类和代理类去实现这个接口里面的方法
代理要有房东的权限 就把房东作为一个属性写进代理类里面 代理类就帮房东实现"出租房屋"的方法 如果代理有自己的方法也能写在后面
测试类的代码
public static void main(String[] args) { Host host = new Host(); Proxy proxy = new Proxy(host); proxy.rent(); }
AOP 面向切面编程
游戏开局10分钟 战绩:0/8/0
游戏结束 战绩:100/8/0
如何完成这一壮举 就需要去看10分钟之后的时间里发生了什么事情 10分钟就是切点 查看的东西就是 表达式 表达式里面写全部的过程
<aop:config> <aop:pointcut id="给切点取个名字 10分钟以后的事" expression="execution(* com.公司名称.本类的包.具体的类.*(..))"/> </aop:config>