SpringStudy
SpringStudy01-HelloSpring
这里本人使用的是maven,所以必须导入maven依赖
下面图是该例子包的结构
创建一个HelloSpring类
public class Hello {
private String str;
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
@Override
public String toString() {
return "Hello{" +
"str='" + str + '\'' +
'}';
}
}
编写beans.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--bean就是java对象 , 由Spring创建和管理,id随便取,class是指注册的类的路径-->
<bean id="hello" class="com.wang.pojo.Hello">
<!-- property是给类中的属性赋值用的,name必须与所赋值属性的名字一样,value是指所赋的值-->
<property name="str" value="Spring"/>
</bean>
</beans>
编写测试类
public class MyTest {
public static void main(String[] args){
//
//获取spring上下文对象
ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
Hello hello=(Hello)context.getBean("hello");//获取bean对象
System.out.println(hello.toString());//输出
}
}
=========================================================
SpringStudy02-ioc01(引用ref)
包的结构
dao层
- UserDao接口
public interface UserDao {
void getuser();
}
- UserDaoImpl类
public class UserDaoImpl implements UserDao {
public void getuser() {
System.out.println("该我表演了!");
}
}
- UserDaoMysqlImpl类
public void getuser() {
System.out.println("改你表演了!");
}
}
service层
- UserService接口
public interface UserService {
void getuser();
}
- UserServiceImpl类
public class UserServiceImpl implements UserService{
private UserDao userdao;
public void setUserdao(UserDao userdao) {
this.userdao = userdao;
}
public void getuser() {
userdao.getuser();
}
}
beans.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--bean就是java对象 , 由Spring创建和管理-->
<bean id="DaoIml" class="com.wang.dao.UserDaoImpl"/>
<bean id="MysqlIml" class="com.wang.dao.UserDaoMysqlImpl"/>
<bean id="ServiceIml" class="com.wang.service.UserServiceImpl">
<!-- 将DaoIml赋值给ServiceIml中的userdao-->
<property name="userdao" ref="DaoIml"></property>
</bean>
<!-- ref:引用spring容器中创建好的对象-->
<!-- value:值, 基本的数据类型-->
<bean id="ServiceIml02" class="com.wang.service.UserServiceImpl">
<property name="userdao" ref="MysqlIml"></property>
</bean>
</beans>
测试类
public class MyTest {
public static void main(String[] args){
//测试是否赋值成功
ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
UserServiceImpl userService=(UserServiceImpl)context.getBean("ServiceIml02");
userService.getuser();
UserService userService1=new UserServiceImpl();
((UserServiceImpl)userService1).setUserdao(new UserDaoImpl());
userService1.getuser();
}
}
注意点
UserServiceImpl类中的userdao类型是UserDao接口,否则类型不一致会导致赋值失败
========================================================
SpringStudy03-构造器注入(有参构造)
- 编写User1类
public class User1 {
private String name1;
private int age;
private String sex;
public User1() {
}//无参构造
public User1(String name1, int age, String sex) {
this.name1 = name1;
this.age = age;
this.sex = sex;
}//此处就是有参构造,本例就是通过有参构造进行赋值
public String getName1() {
return name1;
}
public void setName1(String name1) {
this.name1 = name1;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "User1{" +
"name1='" + name1 + '\'' +
", age=" + age +
", sex='" + sex + '\'' +
'}';
}
}
- 在beans.xml注册bean
1.通过属性类型赋值
<bean id="user1" class="com.wang.pojo.User1">
<constructor-arg type="java.lang.String" value="wdawd"></constructor-arg>
<constructor-arg type="int" value="34"></constructor-arg>
<constructor-arg type="java.lang.String" value="男"></constructor-arg>
</bean>
- 通过下标进行赋值(从0开始)
<bean id="user1" class="com.wang.pojo.User1">
<constructor-arg index="0" value="wdwda"></constructor-arg>
<constructor-arg index="1" value="34"></constructor-arg>
<constructor-arg index="2" value="nan"></constructor-arg>
</bean>
- 通过参数名赋值
<bean id="user1" class="com.wang.pojo.User1">
<constructor-arg name="name1" value="adawd"></constructor-arg>
<constructor-arg name="age" value="34"></constructor-arg>
<constructor-arg name="sex" value="nan"></constructor-arg>
</bean>
=========================================================
SpringStudy04-di注入
首先创建一个Student类,该类中包含各种属性,让我们看看各种属性注入的方法
public class Student {
//属性
private String name;
private Address address;
private String[] books;
private List<String> hobbys;
private Map<String,String> card;
private Set<String> games;
private String wife;
private Properties info;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String[] getBooks() {
return books;
}
public void setBooks(String[] books) {
this.books = books;
}
public List<String> getHobbys() {
return hobbys;
}
public void setHobbys(List<String> hobbys) {
this.hobbys = hobbys;
}
public Map<String, String> getCard() {
return card;
}
public void setCard(Map<String, String> card) {
this.card = card;
}
public Set<String> getGames() {
return games;
}
public void setGames(Set<String> games) {
this.games = games;
}
public String getWife() {
return wife;
}
public void setWife(String wife) {
this.wife = wife;
}
public Properties getInfo() {
return info;
}
public void setInfo(Properties info) {
this.info = info;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address.toString() +
", books=" + Arrays.toString(books) +
", hobbys=" + hobbys +
", card=" + card +
", games=" + games +
", wife='" + wife + '\'' +
", info=" + info +
'}';
}
}
其中的Address类
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Address{" +
"address='" + address + '\'' +
'}';
}
}
下面进行bean注册
- 首先将Adress类注册
<bean id="address" class="com.wang.pojo.Address">
</bean>
- 注册Student类
<bean id="student" class="com.wang.pojo.Student">
<!-- 第一种:普通值注入 value-->
<property name="name" value="we"></property>
<!-- 第二种:bean注入 ref引用 -->
<property name="address" ref="address"></property>
<!-- 数组注入 -->
<property name="books">
<array>
<value>红楼梦</value>
<value>西游记</value>
<value>三国演义</value>
<value>水浒传</value>
</array>
</property>
<!-- List注入 -->
<property name="hobbys">
<list>
<value>打游戏</value>
<value>学习</value>
<value>看书</value>
</list>
</property>
<!-- Map注入 -->
<property name="card">
<map>
<entry key="身份证" value="4141414141414141414"></entry>
<entry key="学号" value="20210903"></entry>
</map>
</property>
<!-- Set注入 -->
<property name="games">
<set>
<value>王者荣耀</value>
<value>和平精英</value>
</set>
</property>
<!-- null值注入 如果为空值注入则value为空 -->
<property name="wife">
<null></null>
</property>
<!-- properties值注入 -->
<property name="info">
<props>
<prop key="学号">20210903</prop>
<prop key="性别">男</prop>
<prop key="姓名">we</prop>
</props>
</property>
</bean>
注意map注入!!!!!!!!
=========================================================
SpringStudy05-p命名注入和c命名注入
首先编写User类
public class User {
private String name;
private String age;
public User() {
}
public User(String name, String age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age='" + age + '\'' +
'}';
}
}
进行p和c命名注入需要在xml文件中beans标签加入
xmlns:p=“http://www.springframework.org/schema/p”
xmlns:c="http://www.springframework.org/schema/c"
如下所示
<?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
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- p命名注入 必须引入xmlns:p="http://www.springframework.org/schema/p -->
<bean id="user" class="com.wang.pojo.User" p:age="20" p:name="we">
</bean>
<!-- c命名注入 必须引用 xmlns:c="http://www.springframework.org/schema/c
c命名注入实际运用构造器注入:constructor-arg
-->
<bean id="user2" class="com.wang.pojo.User" c:name="20" c:age="we1"></bean>
</beans>
=========================================================
SpringStudy06-自动装配(Autowired)
创建Cat4,Dog4,People这三个类
- Cat4.java
public class Cat4 {
public void shout(){
System.out.println("喵~");
}
}
- Dog4.java
public class Dog4 {
public void shout(){
System.out.println("汪~");
}
}
- People4.java
public class People4 {
private Dog4 dog4;
private Cat4 cat4;
private String name;
public Dog4 getDog4() {
return dog4;
}
public void setDog4(Dog4 dog4) {
this.dog4 = dog4;
}
public Cat4 getCat4() {
return cat4;
}
public void setCat4(Cat4 cat4) {
this.cat4 = cat4;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "People4{" +
"dog4=" + dog4 +
", cat4=" + cat4 +
", name='" + name + '\'' +
'}';
}
}
- beans.xml文件
<?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:context="http://www.springframework.org/schema/context"
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">
<bean id="cat4" class="com.wang.pojo4.Cat4"></bean>
<bean id="dog4" class="com.wang.pojo4.Dog4"></bean>
<!-- 使用byName自动装配
byName:会自动在容器上下文中查找和自己对象set方法后面的值所对应的bean id!
即本例中People4类中的set方法参数名的匹配 => 例:setCat4(Cat4 cat4)中的cat与bean中id的匹配
如果<bean id="cat4" class="com.wang.pojo.Cat4"></bean>改为<bean id="cat22" class="com.wang.pojo.Cat4"></bean>
则会出现异常 即名字不重复就不会报错
-->
<bean id="people4" class="com.wang.pojo.People4" autowire="byName">
<property name="name" value="we"></property>
</bean>
<!-- 使用byType自动装配
byType:会自动在容器上下文中查找和自己对象属性类型相同的beanid!
如果<bean id="cat4" class="com.wang.pojo.Cat4"></bean>改为<bean id="cat22" class="com.wang.pojo.Cat4"></bean>
则不会出现异常
但是属性类型必须唯一,否则会报错 =>上例增加一个<bean id="cat11" class="com.wang.pojo.Cat4"></bean>,就会报错(多了一个类型相同的)
使用byType自动装配可以不用命名id 即<bean class="com.wang.pojo.Cat4"></bean>
-->
<bean id="people4" class="com.wang.pojo.People4" autowire="byType">
<property name="name" value="we"></property>
</bean>
</beans>
=========================================================
SpringStudy07-使用注解进行自动装配(@Autowired @Resource)
@Autowire通过byType方式实现
@Resource默认通过byname方式实现,如果找不到则通过byType,都找不到就报错
1. @Autowire
- 首先xml文件有变化
<?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:context="http://www.springframework.org/schema/context"
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">
<!-- @Autowired 通过bytype方式自动装配-->
<!-- 使用注解@Autowired自动装配 -->
<!-- 开启注解支持 这一步必须有-->
<context:annotation-config></context:annotation-config>
<bean id="dog2" class="com.wang.pojo2.Dog2"></bean>
<bean id="=cat2" class="com.wang.pojo2.Cat2"></bean>
<bean id="dog22" class="com.wang.pojo2.Dog2"></bean>
<bean id="cat22" class="com.wang.pojo2.Cat2"></bean>
<bean id="people2" class="com.wang.pojo2.People2">
<property name="name2" value="we2"></property>
</bean>
<!-- 使用注解@Autowired自动装配可以将People2中的set方法去掉,也可以运行成功-->
<!--如果增加<bean id="dog22" class="com.wang.pojo2.Dog2"></bean>
<bean id="=cat22" class="com.wang.pojo2.Cat2"></bean>
可以使用@Qualifier(value="dog22")进行特定自动装配
@Autowired //注释自动装配
@Qualifier(value = "dog22")
private Dog2 dog2;
@Autowired //注释自动装配
@Qualifier(value = "cat22")//指定特定的
private Cat2 cat2;
-->
<!-- 注解自动装配@Resource 它本身先进行id查找 有相同则正常运行 否则报错
其次可以进行类型查找 相同则运行
由此可看出即使查找id不相同 类型相同的话也可以进行自动装配
<bean id="dog2231" class="com.wang.pojo2.Dog2"></bean>
<bean id="=cat21314" class="com.wang.pojo2.Cat2"></bean>
自行进行测试
@Resource(name="")也可以指定特定的id
@Resource相比前两种更加高级
在beans3.xml进行测试
-->
</beans>
- Cat2类
public class Cat2 {
public void shout(){
System.out.println("喵~");
}
}
- Dog2类
public class Dog2 {
public void shout(){
System.out.println("汪~");
}
}
- People2类
public class People2 {
@Autowired //注释自动装配
@Qualifier(value = "dog22")//指定特定的bean对象进行自动装配
private Dog2 dog2;
@Autowired //注释自动装配
@Qualifier(value = "cat22")//指定特定的
private Cat2 cat2;
private String name2;
public Dog2 getDog2() {
return dog2;
}
// public void setDog2(Dog2 dog2) {
// this.dog2 = dog2;
// }
public Cat2 getCat2() {
return cat2;
}
// public void setCat2(Cat2 cat2) {
// this.cat2 = cat2;
// }
public String getName2() {
return name2;
}
public void setName2(String name2) {
this.name2 = name2;
}
@Override
public String toString() {
return "People2{" +
"dog2=" + dog2 +
", cat2=" + cat2 +
", name2='" + name2 + '\'' +
'}';
}
}
2.@Resource
- xml文件
<?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:context="http://www.springframework.org/schema/context"
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">
<!-- @Resource 自动装配 默认通过byname方式实现,如果找不到则通过byType,都找不到就报错-->
<context:annotation-config></context:annotation-config>
<bean id="dog3" class="com.wang.pojo3.Dog3"></bean>
<bean id="cat3" class="com.wang.pojo3.Cat3"></bean>
<bean id="people3" class="com.wang.pojo3.People3"></bean>
</beans>
重点是People3类
public class People3 {
@Resource//注解自动装配
private Dog3 dog3;
@Resource
private Cat3 cat3;
private String name3;
public Dog3 getDog3() {
return dog3;
}
public void setDog3(Dog3 dog3) {
this.dog3 = dog3;
}
public Cat3 getCat3() {
return cat3;
}
public void setCat3(Cat3 cat3) {
this.cat3 = cat3;
}
public String getName3() {
return name3;
}
public void setName3(String name3) {
this.name3 = name3;
}
@Override
public String toString() {
return "People3{" +
"dog3=" + dog3 +
", cat3=" + cat3 +
", name3='" + name3 + '\'' +
'}';
}
}
注意:@Resource(name="")进行特定指定bean对象
=========================================================
SpringStudy08-AOP
动态代理
Java中的动态代理有一个专门的接口InvocationHandler
下面我们使用Java来实现动态代理
- 接口Game
public interface Game {
public void game();
}
- 真实对象(要被代理的对象)
public class UserGame implements Game{
public void game() {
System.out.println("我要卖游戏");
}
}
- 代理工具类(ProxyInvocationHandler)
//工具类 可以动态进行代理,实现InvocationHandler接口的方法
public class ProxyInvocationHandler implements InvocationHandler {
//设置代理接口
private Object ProxyClass;
public void setProxyClass(Object proxyClass) {
ProxyClass = proxyClass;
}
//生成代理类(除了ProxyClass,都是固定)
public Object getProxy(){
return Proxy.newProxyInstance(this.getClass().getClassLoader(),ProxyClass.getClass().getInterfaces(),this);
}
//返回代理类,并返回结果(可以在该类中添加函数:例如 日志)
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//method.getName()获取真实对象中执行的方法名
log(method.getName());
Object result=method.invoke(ProxyClass,args);
return result;
}
public void log(String msg){
System.out.println("执行了"+msg+"方法");
}
}
- 测试类
@Test
public void test(){
UserGame userGame=new UserGame();
ProxyInvocationHandler pih=new ProxyInvocationHandler();
pih.setProxyClass(userGame);
Game game1=(Game) pih.getProxy();//由于动态代理返回的是一个接口,所以用接口来命名
game1.game();
}
- 结果
使用Spring进行动态代理(AOP)
1、使用Spring的API接口
- 创建一个Log类
//MethodBeforeAdvice 前置通知的接口
public class Log implements MethodBeforeAdvice {
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println(o.getClass().getName()+"类的"+method.getName()+"方法被执行了");
}
}
- 创建AfterLog类
//AfterReturningAdvice后置通知的接口
public class AfterLog implements AfterReturningAdvice {
public void afterReturning(Object returnValue, Method method, Object[] objects, Object o1) throws Throwable {
System.out.println("执行了"+method.getName()+"方法,返回结果为:"+returnValue);
}
}
我们可以将上面两个类放在同一个包下
- 真实对象接口UserService
public interface UserService {
public void add();
public void delete();
public void update();
public void select();
}
- 真实对象接口实现类UserServiceImpl
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 select() {
System.out.println("查找一个用户");
}
}
- beans.xml文件
<?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: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">
<bean id="userService" class="com.wang.service.UserServiceImpl"></bean>
<bean id="log" class="com.wang.log.Log"></bean>
<bean id="afterLog" class="com.wang.log.AfterLog"></bean>
<!--方式1:使用spring的API接口-->
<!-- 配置AOP 导入AOP的约束-->
<aop:config >
<!-- 切入点 就相当于动态代理增加的方法,只不过spring更加方便-->
<aop:pointcut id="point" expression="execution(* com.wang.service.UserServiceImpl.*(..))"/>
<!-- 执行环绕增加 advisor 是一个类-->
<aop:advisor advice-ref="log" pointcut-ref="point"></aop:advisor>
<aop:advisor advice-ref="afterLog" pointcut-ref="point"></aop:advisor>
</aop:config>
</beans>
- 测试类
@Test
public void test(){
ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
UserService userService=context.getBean("userService",UserService.class);//由于动态代理返回的是一个接口,所以用接口来命名
userService.add();
}
2、Spring的aspect(切面)
- 真实对象接口UserService
public interface UserService {
public void add();
public void delete();
public void update();
public void select();
}
- 真实对象接口实现类UserServiceImpl
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 select() {
System.out.println("查找一个用户");
}
}
- 自定义一个DiyPointCut类
//自定义切入点类
public class DiyPointCut {
public void before(){
System.out.println("=========执行之前========");
}
public void after(){
System.out.println("=========执行之后========");
}
}
- beans.xml
<?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: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">
<bean id="userService" class="com.wang.service.UserServiceImpl"></bean>
<!--方式2: 自定义切入点类-->
<bean id="diypointcut" class="com.wang.diy.DiyPointCut"></bean>
<aop:config>
<!-- aspect :切面 是一个类 导入含有before和after方法的类-->
<aop:aspect ref="diypointcut">
<!-- 切入点 expression是固定的 是execution-->
<aop:pointcut id="point" expression="execution(* com.wang.service.UserServiceImpl.*(..))"/>
<!-- 通知 调用切面类中的方法-->
<aop:before method="before" pointcut-ref="point"></aop:before>
<aop:after method="after" pointcut-ref="point"></aop:after>
</aop:aspect>
</aop:config>
自行进行测试
=============================================================
还未完善!