1.Spring入门介绍
Spring框架是一个开放源代码的J2EE应用程序框架,由Rod Johnson发起,是针对bean的生命周期进行管理的轻量级容器(lightweight container)。
1.J2SE 基础的java语法
2. j2EE JAVA企业级开发
3.J2ME 移动端java开发 手机QQ java写的!!!
4 .bean: 由Spring容器管理的对象称之为bean!!!
5. 轻量级: 使用spring容器的方式简单/操作便捷.
6 .容器: Spring在内存中开辟了一大块空间,用来管理对象.
IOC介绍
Ioc全称Inversion of Control,即“控制反转”,这是一种设计思想。对象创建的权利由Spring框架完成.由容器管理对象的生命周期.
IOC实现原理
1.获取配置文件名称
2.创建容器对象,spring容器加载配置名称,当按照顺序加载到bean标签时,开始创建对象
3.Spring通过bean标签内的class属性获取类型的路径,然后通过反射机制实例化对象,要求必须无参构造,在Spring中使用反射机制时不能通过含参构造创建对象,因为设计源码过于麻烦
4.bean中的id座位 map中的key值,而实例化的对象保存到map中,当作value,实例化对象创建成功
5,用户需要创建对象时,可以通过 (对象类型.class) 或者 key值获取 使用key值获取时,必须使用强转
Spring注解开发
注解一:@Configuration 作用:标识当前类是配置类
注解二:@Bean 作用:告诉Spring容器,此方法的方法名是map的k值,返回值是Map中的value
注解三:@ComponentScan() //包扫描注解:让spring注解生效
1.7 Spring中的单例和多例问题
1.7.1 概念说明
单例对象: Spring中保存一份对象.
多例对象: Spring容器负责创建多次对象. 内存中对象多份.
Spring对象的生命周期
对象创建
初始化数据
调用方法,完成功能 需要手动调用
对象销毁
核心: 生命周期的方法都是自动调用!!!
@Component/@Bean区别:
- 1.@Component spring容器通过反射自动创建对象
- @Bean 是用户自己手动创建对象
- 2.@Component 标识类的
- @Bean 标识配置类中的方法
- 3.@Component 对象的Id是类名首字母小写
- @Bean 对象的Id是方法名
@Component //spring会自动会标注此注解的类进行反射实例化对象,然后交给容器保管
public class Demo1 {
public Demo1(){
System.out.println("对象的创建");
}
@PostConstruct
public void run(){
System.out.println("对象进行初始化操作,给对象赋值");
}
public void play(){
System.out.println("业务调用,需要主动调用");
}
@PreDestroy
public void dis(){
System.out.println("释放资源前,调用此方法,释放资源");
}
@Configuration //标志此类是一个配置类
@ComponentScan("cn.tedu.exercise1") //标志路径中的类的注解全生效
public class SpringConfig {
}
//使用user类
public class User1 {
public static void main(String[] args) {
//使用注解方式创建spring对象
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
//通过容器对象提取对象
Demo1 demo1 = context.getBean(Demo1.class);
//使用对象调用方法
demo1.play();
//销毁对象,是一个严谨的操作,需要使用接口的实现类
context.close();
}
}
Spring中依赖注入
说明: 依赖注入就是将Spring容器中管理对象(数据),赋值给对象的属性.
核心机制: 如果需要使用依赖注入,则对象的属性必须有setxxx()方法
第一种方式:通过配置文件依赖注入
<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">
<!--
知识点讲解:
1.bean 被spring容器管理的对象称之为bean
2.id 全局唯一变量一般类型首字母小写
3.class class的路径信息,需要写全名
-->
<bean id="dog" class="cn.tedu.exercise2.Dog"></bean>
<bean id="person" class="cn.tedu.exercise2.Person">
<property name="id" value="12"></property> //给属性赋值
<property name="name" value="小明"></property>
<property name="dog" ref="dog"></property> //将对象赋值给属性,使用ref
</bean>
</beans>
//定义Person类
public class Person {
private Integer id; //定义私有属性,并调用set方法
private String name;
private Dog dog;
public void play(){
dog.jump();
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void run(){
System.out.println(name+id+"岁就开始跑步了");
}
}
通过构造方法注入
<!--1.让spring容器管理car 参数的个数 决定调用的构造方法 -->
<bean id="car" class="com.jt.demo7_di.Car">
<constructor-arg name="name" value="奔驰大G"/>
<constructor-arg name="color" value="绿色"/>
</bean>
2.在car类中提供构造方法
第二种方式:通过注解方法
1.设置接口 Animal 并设置 抽象方法
2.设置实现类,并重写抽象方法, 使用@component 标记此类
3.定义Person类
@Component //spring会自动会标注此注解的类进行反射实例化对象,然后交给容器保管
public class Person {
@Autowired //自动注入
/**实现原理:@Autowired 默认使用set注入,按照类型匹配
* 也可以使用name名字匹配:根据spring中的key进行注入,一般用于有多个实现类
* */
private Animal animal;
public void eat(){
animal.eat();
}
}
4.创建一个配置类
@Configuration //标志此类是一个配置类
@ComponentScan("cn.tedu.exercise3") //标志路径中的类的注解全生效
public class SpringConfig {
}
public class PersonTest {
public static void main(String[] args) {
//使用注解方式启动容器
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
Person person = context.getBean(Person.class);
person.eat();
}
}
Spring属性赋值-@Value注解
#1.数据结构 key=value
#2.其中的数据默认都是String类型,数值可以切换为数值类型
#3.程序读取时,默认的字符集ISO-8859-1 不支持中文,
如果支持中文,使用utf-8
@Component
@PropertySource(value = "test.properties",encoding = "utf-8")
public class Test {
@Value("${dept.id}") //可以使用@value直接赋值,但是作用很小,使用propertise配置文件赋值
private Integer id;
@Value("${dept.name}")
private String name;
public void run(){
System.out.println(name+"臂力有"+id+"万斤");
}
}