Spring框架
IOC AOP SSM
不重复造轮子,spring不是一个实现特定功能的框架,是一个框架体系,其中Spring-Core是核心模块,IoC功能就是这个模块的,控制反转
学Spring干什么,学会IoC的使用流程,项目中用到的功能,有什么好的插件或框架,就拿过来,让spring管理它的功能对象。
IOC使用
IoC做的事情,就是管理项目中的对象
在applicationContext.xml中声明一个bean
<bean id="dcs" class="entity.Student">
之后在Java代码中,通过bean的id调用
想要注入属性,需要加property标签
<property name="sname" value="段朝生"/>
<property name="yanzhi" value="5"/>
Spring有setter方式注入(上面就是),p命名空间注入,构造注入,注解注入
p命名空间注入,本质和setter完全一样,就是少写一点点
先导入功能
applicatuonContext:
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/p
http://www.springframework.org/schema/p/spring-p-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<bean id="dcs" class="entity.Student">
<property name="sname" value="段朝生"/>
<property name="yanzhi" value="5"/>
<property name="chengji">
<ref bean="cj3"/>
</property>
<property name="hobby">
<list>
<value><![CDATA[学&习]]></value>
<value>看&书</value>
<value>游戏</value>
</list>
</property>
<property name="ex">
<set>
<value>aaa</value>
<value>bbb</value>
<value>ccc</value>
</set>
</property>
<property name="qianren">
<map>
<entry>
<key><value>1</value></key>
<value>zhurui</value>
</entry>
<entry>
<key><value>2</value></key>
<value>zhiling</value>
</entry>
<entry>
<key><value>3</value></key>
<value>zhurui</value>
</entry>
</map>
</property>
</bean>
<bean id="cj2" class="entity.Score" p:bishi="11" p:jishi="22" p:total="33"/>
<bean id="cj3" class="entity.Score">
<constructor-arg name="bishi" value="12"/>
<constructor-arg name="jishi" value="32"/>
</bean>
<bean id="cj" class="entity.Score">
<property name="bishi" value="22"/>
<property name="jishi" value="32"/>
<property name="total" value="54"/>
</bean>
</beans>
Student:
package entity;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Student {
private String sname;
private int yanzhi;
private Score chengji;
private List<String> hobby;
private Map<Integer,String>qianren;
public Map<Integer, String> getQianren() {
return qianren;
}
public void setQianren(Map<Integer, String> qianren) {
this.qianren = qianren;
}
public List<String> getHobby() {
return hobby;
}
public void setHobby(List<String> hobby) {
this.hobby = hobby;
}
public Set<String> getEx() {
return ex;
}
public void setEx(Set<String> ex) {
this.ex = ex;
}
private Set<String> ex;
public Score getChengji() {
return chengji;
}
public void setChengji(Score chengji) {
this.chengji = chengji;
}
public void eat() {
System.out.println(this.sname+" is eatting");
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public int getYanzhi() {
return yanzhi;
}
public void setYanzhi(int yanzhi) {
this.yanzhi = yanzhi;
}
}
Score:
package entity;
public class Score {
int bishi,jishi,total;
public Score(){
}
public Score(int bishi,int jishi){
this.bishi=bishi;
this.jishi=jishi;
}
public int getBishi() {
return bishi;
}
public void setBishi(int bishi) {
this.bishi = bishi;
}
public int getJishi() {
return jishi;
}
public void setJishi(int jishi) {
this.jishi = jishi;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
}
Test:
package app;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import entity.Student;
import java.util.List;
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Student s1 = (Student)ac.getBean("dcs");
s1.eat();
System.out.println(s1.getYanzhi());
System.out.println(s1.getChengji().getTotal());
List<String> hobby = s1.getHobby();
System.out.println("爱好:");
for (String string:hobby){
System.out.println(string);
}
System.out.println("前任:");
for (String string : s1.getEx()){
System.out.println(string);
}
System.out.println("有顺序的前任");
for (Integer i:s1.getQianren().keySet()){
System.out.println("第"+i+"个女朋友是:"+s1.getQianren().get(i));
}
}
}
AOP(面向切面编程)
面向过程,面向对象,面向接口,面向切面
出去玩
戴口罩,穿外套,坐车,玩,坐车,托外套,摘口罩
出去吃
测体温,戴口罩,穿外套,走,吃饭,走,托外套,摘口罩
先确保配置文件中,引入功能
总共有三种方式实现AOP
1.使用API(最麻烦)
想要其他的增强,得再写一个类。通过配置文件,将这两个增强方法,织入到目标方法中
前提是,先有这几个bean
2.使用API的自定义方式
还是同一个目标方法,你要写的增强内容可以自由定义在某个类中
3.通过注解方式
配置文件中,直接创建这个bean,再声明一句使用注解方式