又是美好而努力的一天呀~
__
/|
* * * * * * /
* * * / *
* * * / *
* * *
* *
* happy valentine's day *
* *
* *
* *
* *
* / *
/ * *
__/ * *
/| *
目录
ALT+Insert可以重新toString()方法
后面就是测试类里面可以不用进行强转,而是通过在其后面加入类名.class
context.getBean("User",User.class)
1.C命名和P命名空间注入
- P命名注入
- C命名注入

在官网里面

2.bean作用域

这里的话,单例 模式适用于在单线程的情况下,而单例模式适用于多线程
3.自动装配Bean

3.1、byName
会自动在容器上下文中查找,和自己对象set方法后面的值对应的bean id

3.2、byType
会自动在容器上下文中查找,和自己对象属性类型相同的bean

3.注解装配
配置:
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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
</beans>
aop和注解的配置 注意看是有很多相似的地方的。

autowired



Resoures

4.Spring注解开发
总结:这一部分的笔记截取的比较多,所以看起来会有点'乱',但是大致的知识点也就是这样了。
首先 就是在beans中导入注解相关的依赖,然后 知道@Component以及其衍生注解,@Value ,有自动装配功能的**@Autowired,@Nullable,@Resource** 注解的功能。接着 就是其作用域,单例模式,原型模式,用**@Scope** 。最后就是要知道xml和注解的区别。
在spring4之后,要使用注解开发,必须保证aop的导入了使用注解需要导入context约束,增加注解的支持!
4.1、bean
首先就是配置 aop和context的导入非常相似

4.2、属性如何注入
Component注解,表示组件的意思,

4.3、衍生的注解

4.4、自动装配

4.5、作用域

4.6、小结

5.使用JavaConfig实现配置
这里就是完全使用注解了,小案例如下:
代码:
java
//1.ZhangConfig类
package org.example.Config;
import org.example.pojo.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("org.example.pojo")
public class ZhangConfig {
@Bean
public User getUser(){
return new User();
}
}
//2.User类
package org.example.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class User {
@Value("zhang")
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
//3.测试类
import org.example.Config.ZhangConfig;
import org.example.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class test {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(ZhangConfig.class);
User getUser = context.getBean("getUser", User.class); //这里的getUser就是User类里面的方法名
System.out.println(getUser.getName());
}
}

下面两张图是狂神 老师的代码讲解:


6.ERROR



就是我的那个Configuration没有配置好,呜呜呜,我用了一个小时才发现!
7.代理模式
7.1、静态代理模式
主要就是集中体现第三方的功能,这里其实很好理解,看代码就行。
案例1:增删查改,且附加了一个增加日志的功能
有四个类:

1、User是主程序
2、UserDao是接口,里面是四个方法:增删查改
3、UserDaolmpl实现了UserDao接口类
4.UserDaoService是静态代理类,在保证不改动原有实现类的基础上,添加其他的功能。
代码如下:
java
//1.User类
package com.zhang.dao;
public class User {
public static void main(String[] args) {
UserDaoService userDaoService=new UserDaoService();
UserDaolmpl userDaolmpl=new UserDaolmpl();
userDaoService.setUserDaolmpl(userDaolmpl);
userDaoService.add();
}
}
//2.UserDao类
package com.zhang.dao;
public interface UserDao {
void add();
void delete();
void update();
void query();
}
//3.UserDaolmpl类
package com.zhang.dao;
public class UserDaolmpl implements UserDao{
@Override
public void add() {
System.out.println("添加!");
}
@Override
public void delete() {
System.out.println("删除!");
}
@Override
public void update() {
System.out.println("更改!");
}
@Override
public void query() {
System.out.println("查找!");
}
}
//4.UserDaoService类
package com.zhang.dao;
//添加日志功能
public class UserDaoService {
private UserDaolmpl userDaolmpl;
public void setUserDaolmpl(UserDaolmpl userDaolmpl) {
this.userDaolmpl = userDaolmpl;
}
public void add() {
userDaolmpl.add();
log("添加");
}
public void query() {
userDaolmpl.query();
log("查找");
}
public void delete() {
userDaolmpl.delete();
log("删除");
}
public void update() {
userDaolmpl.update();
log("更改");
}
//添加日志函数
public void log(String msg){
System.out.println("使用了"+msg+"方法");
}
}
另外一个案例是买房案例,但是我觉得两者差不多,所以这里就不摆出来了,有需要的宝子和我说。
7.2、动态代理模式
狂神老师说了一句很有意思的一句话,永远不要在学生听不懂的地方过多停留。
言归正传,我觉得动态和静态相比,动态与其名字一样,很灵活,非常的方便。
案例1:
这个主要就是理解代码的作用:
工具类的思想
1.动态代理类
java
package org.example.demo02;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class ProxyInvocationHandler implements InvocationHandler {
//1.被代理的接口
private Object target;
public void setTarget(Object target) {
this.target = target;
}
//2.生成得到代理类
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()); //得到当前方法的名字
Object result=method.invoke(target,args);
return result;
}
//添加一个日志方法
public void log(String msg){
System.out.println("使用了"+msg+"方法");
}
}
2.测试类:




反射机制是什么呀!(没学,呜呜呜~)
7.2.3、Error
Exception in thread "main" java.lang.ClassCastException: class jdk.proxy1.Proxy0 cannot be cast to class org.example.demo01.Poxy (jdk.proxy1.Proxy0 is in module jdk.proxy1 of loader 'app'; org.example.demo01.Poxy is in unnamed module of loader 'app')
at org.example.demo02.Client.main(Client.java:13)

解决:
我的问题有二
其一是在主程序里面引用的内容,也就是Proxy,是我上面一个包的类
其二是我没搞清楚代码的整个意思,所以就算是对照着狂神老师的代码也没瞧见错误。
更改如下:

画红线的部分要注意,getProxy()返回的是接口,而不是实体类。

批量替换快捷键ctr+r
最后,祝大家身体健康,一天更比一天强!!!