20191012
官网:https://spring.io/projects/spring-framework#overview
官方下载地址: http://repo.spring.io/release/org/springframework/spring
GitHub:https://github.com/spring-projects/spring-framework
org.springframework
spring-webmvc
5.2.0.RELEASE
org.springframework
spring-jdbc
5.2.0.RELEASE
Spring是一个轻量级控制反转(IOC)和面向切面编程(AOP)的框架
优点:
构建 协调 连接
约定
开发人员只需要规定不符合约定的地方配置
没有规定用配置的地方就用默认的配置,力求配置的简洁化,规定需要用配置的地方就使用自定义配置Spring Boot中的约定?
- Meavn结构目录,resources中以前存放的是配置文件,现在,java+resources以及默认的编译生成的类都在targe文件夹下面
- Spring boot命名的文件必须是application.yml文件或者.properties文件,且唯一
- application.yml中默认属性。数据库连接信息必须是以spring: datasource: 为前缀;多环境配置。该属性可以根据运行环境自动读取不同的配置文件;端口号、请求路径等
控制反转:控制权从从程序员手中回到了用户手中
一种设计思想(不要打电话了我们会打给你的),你的对象不由你控制,由容器控制(Spring),id(依赖注入)实现了这种方法
谁控制谁,控制什么?
传统的控制:我们直接在对象的内部进行new 对象,程序主动去创建依赖
loc 容器控制对象,控制了外部资源(对象/文件)
正转?有反转就有正转:程序在对象的内部进行new 对象,程序主动去创建依赖
为什么是反转?是容器帮我们注入依赖了对象,对象是被依赖
反转了什么?依赖对象的获取
UserDao 接口
UserDaoImpl 实现类
UserService 业务接口
UserServiceImpl 业务实现类
需要改的话得回到Service中,现在只需要在test中进行修改
org.springframework
spring-webmvc
5.1.10.RELEASE
org.springframework
spring-jdbc
5.2.0.RELEASE
package com.lala.pojo;
/**
* @Description:TODO
* @Author:@李小白
* @Date:2019/10/12 10:57
*/
public class User {
// 有参构造进行默认的实现
private String name;
public User(String name) {
this.name = name;
System.out.println("user有参构造");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void show(){
System.out.println("show"+name);
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
'}';
}
}
package com.lala.pojo;
/**
* @Description:TODO
* @Author:@李小白
* @Date:2019/10/12 10:57
*/
public class User2 {
// 无参构造,默认实现
private String name;
public User2() {
System.out.println("无参构造");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User2{" +
"name='" + name + '\'' +
'}';
}
}
有参构造和无参构造的区别就是
constructor-arg 有参
property 无参
beans.xml
import com.lala.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @Description:TODO
* @Author:@李小白
* @Date:2019/10/12 11:02
*/
public class MyText {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml.xml");
User user = (User) context.getBean("user");
user.show();
System.out.println(user.toString());
}
}
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.westos.pojo.UserOrcialDaoImpl;
import org.westos.service.UserService;
import org.westos.service.UserServiceImpl;
/**
* @Description:IOC理论第一个,再进行改良
* @Author:@李小白
* @Date:2019/10/9 22:09
*/
public class MyTest {
public static void main(String[] args) {
//原始的进行获取
//UserService userService = new UserServiceImpl();
//1.获取ApplicationContext对象,拿到容器
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//在里面进行改动你要输出的数据
//((UserServiceImpl) userService).setUserDao(new UserOrcialDaoImpl());
//userService.getUser();
//容器在手,天下我有,需要什么就get什么,,(里面的是bean的名字)
UserServiceImpl userServiceImpl = (UserServiceImpl) context.getBean("applicationContext");
//调用方法
userServiceImpl.getUser();
//在获取的时候也不用这样的进行获取了,就得到String的上下文的对象,并且调用相应的beans
//调用beans的名字,强转成他所需要的类型
}
}
三种创建方法,最常用name
方式一
方式二
public class Persion {
private Man man;
private String sex;
public Man getMan() {
return man;
}
public void setMan(Man man) {
this.man = man;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Persion{" +
"man=" + man +
", sex='" + sex + '\'' +
'}';
}
}
public class Man {
private String sex;
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
ApplicationContext context = new ClassPathXmlApplicationContext("beans1.xml");
Persion persion = (Persion) context.getBean("persion");
System.out.println(persion.toString());
Persion{man=com.lala.pojo.Man@100d6b1, sex='男'}
@Data
public class Student {
private String name;
private String[] books;
private List hobbys;
private Map card;
private Set games;
private String wife;
private Properties info;
}
beans
我在未来等你
愿风裁尘
宅在家里
和喜欢的在一起
宅在家里
都是数据
都是数据
driver
url
username
password
结果
Student{name='李强娜', books=[我在未来等你, 愿风裁尘], hobbys=[宅在家里, 和喜欢的在一起, 宅在家里], card={章晗=李强娜, 名字=不知道, 密码=521}, games=[都是数据], wife='null', info={password=password, url=url, driver=driver, username=username}}
单例模式 (Spring默认机制)
原型模式:每次从容器中get的时候,都会产生一个新对象!
其余的 request、session、application、这些个只能在web开发中使用到!
在Spring中有三种装配的方式
优点:减少程序员去定义,让程序更加简单灵活
优点:解决xml 带来的弊端
优点:初学者易懂,任何场合都可以使用
缺点,难维护,里面的代码不是自己的
简单,有效的减少代码的量
缺点:不是自己的类不可以使用
pojo
Cat
public class Cat {
public void voice(){
System.out.println("喵喵喵!");
}
}
Dog
public class Dog {
public void voice(){
System.out.println("汪汪汪!");
}
}
Animal
public class Animal {
private String name;
private Cat cat;
private Dog dog;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Cat getCat() {
return cat;
}
public void setCat(Cat cat) {
this.cat = cat;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
@Override
public String toString() {
return "Animal{" +
"name='" + name + '\'' +
", cat=" + cat +
", dog=" + dog +
'}';
}
}
bean
测试文件
ApplicationContext context = new ClassPathXmlApplicationContext("base.xml");
Animal animal = (Animal) context.getBean("animal");
animal.getCat().voice();
animal.getDog().voice();
byName: 会自动在容器上下文中查找,和自己对象set方法后面的值对应的 beanid!
-->
id 可以省略
前提是 和 set 后面的name 唯一
1.可以不用写ref
2.id 写错的情况
3.可以直接不用写id
可以删掉 id 只要calss唯一
小结:
== jdk1.5支持的注解,Spring2.5就支持注解了!==
要使用注解须知:
直接在属性上使用即可!也可以在set方式上使用!
使用Autowired 我们可以不用编写Set方法了,前提是你这个自动装配的属性在 IOC(Spring)容器中存在,且符合名字byname!
表示可以为空
如果@Autowired自动装配的环境比较复杂,自动装配无法通过一个注解【@Autowired】完成的时候、我们> 可以使用@Qualifier(value=“xxx”)去配置@Autowired的使用,指定一个唯一的bean对象注入!
放在属性上,起别名@Resource(name = “别名”),
使用Autowired 我们可以不用编写Set方法了,前提是你这个自动装配的属性在 IOC(Spring)容器中存在,且符合名字byname!
1 配置文件约束
== @Autowired装配的思想就是byType,class唯一,所以删掉id也可以使用==
2 Animal
public class Animal implements Serializable {
@Autowired
private String name;
@Autowired
private Cat cat;
private Dog dog;
public Animal(){
}
}
@Autowired也可以放在set的上面
测试
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("base.xml");
Animal animal = (Animal) context.getBean("animal",Animal.class);
animal.getCat().voice();
animal.getDog().voice();
1.xml
2 Animal
public class Animal implements Serializable {
@Resource
private String name;
@Resource
private Cat cat;
private Dog dog;
public Animal(){
}
}
注意:@Resource(name = “dog”)中的name对应的是xml中的bean id
3 测试
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("base.xml");
Animal animal = (Animal) context.getBean("animal",Animal.class);
animal.getCat().voice();
animal.getDog().voice();
小结:
@Component
public class Student implements Serializable {
@Value("18")
private int age;
@Value("王五")
private String name;
}
@Component表示该类已经被Spring托管了
@value可以理解为原来的xml中的bean中属性注入属性
我们现在要完全不使用Spring的xml配置了,全权交给Java来做!
JavaConfig 是Spring的一个子项目,在Spring 4 之后,它成为了一个核心功能!
//这里这个注解的意思,就是说明这个类被Spring接管了,注册到了容器中
@Component
public class Student implements Serializable {
@Value("18")
private int age;
@Value("王五")
private String name;
配置类
@Configuration
//@Configuration 表示该类是个配置类,其实本身就是一个组件
@ComponentScan(value ="com.g.pojo")
//@ComponentScan 扫描该包下的组件
public class BeanConfig {
@Bean
//注册一个bean,相当于我们之前的写的bean标签
//定义的方法名相当于bean中的id的值
//返回值的类型类相当于bean标签中的calss的值
public Student student(){
return new Student();
}
}
测试类
public class MyTest {
public static void main(String[] args) {
//如果完全使用了配置类方式去做,我们就只能通过 AnnotationConfig 上下文来获取容器,通过配置类的class对象加载!
ApplicationContext context = new AnnotationConfigApplicationContext(KuangConfig.class);
User getUser = (User) context.getBean("user");
System.out.println(getUser.getName());
}
}