周口高端网站建设,建设网站的五个步骤是,网上企业推广,wordpress_百科目录
前言
Spring基础与核心概念
Spring是什么
1、什么是容器
2、什么是IoC
3、理解SpringIoC
4、DI#xff08;依赖注入#xff09;
Spring的创建和使用
1、创建Spring项目
1.1、创建一个普通Maven项目
1.2、添加Spring框架支持
1.3、添加启动类和main方法
2、…目录
前言
Spring基础与核心概念
Spring是什么
1、什么是容器
2、什么是IoC
3、理解SpringIoC
4、DI依赖注入
Spring的创建和使用
1、创建Spring项目
1.1、创建一个普通Maven项目
1.2、添加Spring框架支持
1.3、添加启动类和main方法
2、存储Bean对象
2.1、创建Bean对象
2.2、将Bean对象存储到Spring当中
3、获取并使用Bean对象
3.1、先得到Spring上下文对象
3.2、从Spring中取出Bean对象
3.3、使用Bean
Spring更简单的读取和存储对象
1、存储Bean对象
1.1、配置扫描路径
1.2、添加注解存储Bean对象
2、获取Bean对象
2.1、属性注入
2.2、构造方法注入
2.3、Setter注入
2.4、使用Resource另一种注入方法实现
Bean作用域和生命周期
1、Bean作用域定义
1.1、singleton单例作用域
1.2、prototype原型作用域/多例作用域
1.3、request请求作用域
1.4、session回话作用域
1.5、applicatioon全局作用域
1.6、设置作用域
2、Spring执行流程和Bean的生命周期
2.1、Spring执行流程
2.2、Bean生命周期 前言
Spring框架是一个开放源代码的J2EE应用程序框架由Rod Johnson发起是针对bean的生命周期进行管理的轻量级容器lightweight container。 Spring解决了开发者在J2EE开发中遇到的许多常见的问题提供了功能强大IOC、AOP及Web MVC等功能。Spring可以单独应用于构筑应用程序也可以和Struts、Webwork、Tapestry等众多Web框架组合使用并且可以与 Swing等桌面应用程序AP组合。
Spring基础与核心概念
Spring是什么
Spring指的是Spring FrameworkSpring框架它是一个开源框架。Spring支持广泛的应用场景它可以让java的企业级的应用程序开发起来更简单
Spring是包含了众多工具方法的IoC容器
1、什么是容器
容器是用来容纳某种物品的基本装置
2、什么是IoC
Inversion of Control控制反转
传统代码
public class App {public static void main(String[] args) {Car carnew Car(30);car.init();}
}public class Car {private Framework framework;public Car(int size){frameworknew Framework(size);}public void init(){System.out.println(执行了car init方法);//依赖车身framework.init();}
}public class Framework {private Bottom bottom;public Framework(int size){bottomnew Bottom(size);}public void init(){System.out.println(执行了framework init方法);//依赖底盘bottom.init();}
}public class Bottom {private Tire tire;public Bottom(int size){tirenew Tire(size);}public void init(){System.out.println(执行了buttom init方法);//依赖轮胎tire.init();}
}public class Tire {private int size20;public Tire(int size){this.sizesize;}public void init(){System.out.println(执行了轮胎初始化方法sizethis.size);}
}
改进后的代码
public class App {public static void main(String[] args) {Tire tirenew Tire(23);Buttom buttomnew Buttom(tire);FrameWork frameWorknew FrameWork(buttom);Car carnew Car(frameWork);car.init();}
}public class Car {private FrameWork frameWork;public Car(FrameWork frameWork){this.frameWorkframeWork;}public void init(){System.out.println(执行car);//依赖车身frameWork.init();}
}public class FrameWork {public Buttom buttom;public FrameWork(Buttom buttom){this.buttombuttom;}public void init(){System.out.println(执行framework);//依赖车底buttom.init();}
}public class Buttom {private Tire tire;public Buttom(Tire tire){this.tiretire;}public void init(){System.out.println(执行buttom);//依赖轮胎tire.init();}
}public class Tire {private int size23;public Tire(int size){sizethis.size;}public void init(){System.out.println(轮胎-size:size);}
}
当最底层代码改动之后整个调用链上的所有代码都需要修改解决传统开发中的缺陷
3、理解SpringIoC
Spring是一个IoC控制反转容器具备两个最基础的功能将对象存入到容器从容器中取出对象。其最核心的功能就是如何将对象存入到Spring中再从Spring中获取对象的过程 将对象存放到容器中的好处:将对象存储在loC容器相当于将以后可能用的所有工具制作好都放到仓库中需要的时候直接取就行了用完再把它放回到仓库。而new对象的方式相当于每次需要工具了才现做用完就扔掉了也不会保存下次再用的时候还得重新做这就是IoC容器和普通程序开发的区别。 4、DI依赖注入
在程序运行期间动态地将某个对象引入到当前的机制或行为 从广义来说IoC设计思想DI具体的实现技术从不同的维度来描述同一问题 Spring的创建和使用
1、创建Spring项目
1.1、创建一个普通Maven项目 1.2、添加Spring框架支持 1.3、添加启动类和main方法
Maven项目导入jar和设置国内源的方法
配置国内源 配置和检测 setting.xml maven项目下载jar失败的解决方案 没有配置国内源 删除本地仓库的所有jar包重写下载 网络运营商访问国内源接口出现问题 2、存储Bean对象
2.1、创建Bean对象
public class User {public String exo(){return baekhyun;}
}
2.2、将Bean对象存储到Spring当中
在resources下创建一个spring配置文件 将Bean对象配置到spring配置文件中
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contenthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsdbean iduser classUser/bean
/beans bean iduser classUser/beanid中是bean对象名称 class中是对象本身包名类名 3、获取并使用Bean对象
3.1、先得到Spring上下文对象 或者
BeanFactory beanFactorynew XmlBeanFactory(new ClassPathResource(spring-config.xml));
3.2、从Spring中取出Bean对象
User user (User) context.getBean(user);
3.3、使用Bean
System.out.println(user.exo()); 获取Bean的方式 1、根据名称获取Bean User userUsercontext.getBean“user” 2、根据Bean类型来获取Bean User usercontext.getBeanUser.class 3、根据Bean名称Bean类型来获取Bean User usercontext.getBean“user”,User.class ApplicationContext和BeanFactory 相同点 1、都是可以得到Spring上下文对象 2、都是来自Spring的顶级接口 不同点 1、继承关系和功能方面来说: Spring容器有两个顶级的接口: BeanFactory和ApplicationContext。ApplicationContext 属于BeanFactory的子类其中BeanFactory提供了基础的访问Bean的能力。ApplicationContext除了继承了BeanFactory 的所有功能之外它还包含更多的功能如国际化支持、资源访问、事件传播等。 2、从性能方面来说: ApplicationContext 是一次性加载并初始化所有的Bean对象而BeanFactory 是需要哪个Bean才去加载Bean对象因此更加轻量。 Spring更简单的读取和存储对象
1、存储Bean对象
1.1、配置扫描路径
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contenthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsdcontent:component-scan base-packagecom.demo.component/content:component-scanbean iduser classUser/bean
/beans
1.2、添加注解存储Bean对象
类注解Controller控制器、Service服务、Repository仓库、Component组件、Configuration配置
方法注解Bean将当前修饰方法的方法对象存储到Spring当中
方式一类注解
1.2.1、Controller控制器存储
Controller
public class ArticleController {public String sayHi(){return hello,controller;}
} ArticleController articleControllercontext.getBean(articleController,ArticleController.class);System.out.println(articleController.sayHi()); 1.2.2、Component组件
Component
public class UserComponent {public String sayHi(){return hi,component;}
} UserComponent userComponentcontext.getBean(userComponent,UserComponent.class);System.out.println(userComponent.sayHi()); 项目中没有目录所有的类都写在Java根路径下
content:component-scan base-package**/content:component-scan
1.2.3、Service服务
Service
public class ArticleController {public String sayHi(){return hello,controller;}
} 1.2.4、Repository仓库
Repository
public class ArticleController {public String sayHi(){return hello,controller;}
}
1.2.5、Configuration配置
Configuration
public class ArticleController {public String sayHi(){return hello,controller;}
} 五大类注解用途 1、Controller控制器归属于业务逻辑层用来控制用户的行为它用来检查用户参数的有效性 2、Service服务归属于服务层调用持久化类实现相应的功能不直接和数据库交互类似于控制中心 3、Repository仓库归属于持久层是直接和数据库进行交互的。通常每一个表都会对应一个Repository 4、Configuration配置归属于配置层是用来配置当前项目的一些信息 4、Component组件归属于公共工具类提供某些公共方法 调用流程如下 方式二方法注解
将返回的对象存储到Spring当中 注意事项Bean一定要配合五大类注解 public class Student {private int id;private String name;private int age;Overridepublic String toString() {return Student{ id id , name name \ , age age };}public int getId() {return id;}public void setId(int id) {this.id id;}public String getName() {return name;}public void setName(String name) {this.name name;}public int getAge() {return age;}public void setAge(int age) {this.age age;}
}
Component
public class StudentBeans {Beanpublic Student student(){Student stunew Student();stu.setId(04);stu.setName(baekhyun);stu.setAge(30);return stu;}
} Student studentcontext.getBean(student,Student.class);System.out.println(student); 或
Component
public class StudentBeans {Bean(name {s1,s2})public Student student(){Student stunew Student();stu.setId(04);stu.setName(baekhyun);stu.setAge(30);return stu;}
} Student studentcontext.getBean(s1,Student.class);System.out.println(student); 当给Bean设置了name属性之后使用原方法名就不能获取到对象了只能使用设置的名称才能获取
2、获取Bean对象
获取bean对象的过程也叫做对象装配是把对象取出来放到某个类中其也叫做对象注入
2.1、属性注入
Autowired
Controller
public class StudentController {//1、使用属性注入获取beanAutowiredprivate StudentService studentService;public void sayHi(){//调用service方法studentService.sayHi();}
}
Service
public class StudentService {public void sayHi(){//System.out.println(hi,service);}
} StudentController sccontext.getBean(studentController,StudentController.class);sc.sayHi();
优点实现简单
缺点不能注入不可变final对象 只能适用于IoC容器 针对对象是类容易违背单一设计原则
2.2、构造方法注入 //3、构造方法注入private StudentService studentService;Autowiredpublic StudentController(StudentService studentService){this.studentServicestudentService;} 如果当前类中只有一个构造方法可以省略Autowired
优点可以注入不可变对象 //3、构造方法注入private final StudentService studentService;public StudentController(StudentService studentService){this.studentServicestudentService;} 注入对象不会被修改原因加了final修饰符构造方法是随着类加载只执行一次的不像set有可能执行多次被修改的风险 注入对象会被完全初始化 通用性更好
缺点没有属性注入实现简单
2.3、Setter注入 //2、set注入private StudentService studentService;Autowiredpublic void setStudentService(StudentService studentService){this.studentServicestudentService;}
优点更加符合单一设计原则针对对象方法级别
缺点不能注入不可变对象 注入的对象可被修改set方法是普通set方法可以被重复调用在被调用时就存在修改的风险
2.4、使用Resource另一种注入方法实现 Resourceprivate StudentService studentService; Resource和Autowired 相同点都是用来实现依赖注入的 不同点 1、功能支持不同Autowired支持属性注入、setter注入、构造方法注入Resource支持属性注入和setter注入但不支持构造方法注入 2、出身不同Autowired来自Spring框架Resource来自于JDK 3、参数支持不同Autowired只支持required参数Resource支持更多的参数设置 Bean作用域和生命周期
1、Bean作用域定义
Bean在整个Spring框架项目中的某种行为模式
1.1、singleton单例作用域
描述:该作用域下的Bean在loC容器中只存在一个实例:获取Bean(即通过applicationContext.getBean等方法获取)及装配Bean(即通过Autowired注入)都是同一个对象
场景:通常无状态的Bean使用该作用域。无状态表示Bean对象的属性状态不需要更新
备注:Spring默认选择该作用域
Controller
public class UserController {Autowiredprivate User user1;public void getUser(){System.out.println(user1:user1);User uuser1;u.setName(边伯贤);System.out.println(u:u);}
}
Controller
public class UserAdviceController {Resourceprivate User user1;public void getUser(){System.out.println(user1:user1);}
}
Component
public class UserBeans {Beanpublic User user1(){User usernew User();user.setId(4);user.setName(baekhyun);user.setPassword(30);return user;}
} UserController userControllercontext.getBean(userController,UserController.class);userController.getUser();UserAdviceController userAdviceControllercontext.getBean(userAdviceController,UserAdviceController.class);userAdviceController.getUser(); 1.2、prototype原型作用域/多例作用域
描述:每次对该作用域下的Bean的请求都会创建新的实例:获取Bean(即通过applicationContext.getBean等方法获取)及装配Bean(即通过Autowired注入)都是新的对象实例
场景:通常有状态的Bean使用该作用域
1.3、request请求作用域
描述每次http请求会创建新的Bean实例类似于prototype
场景一次http的请求和响应的共享Bean
备注限定SpringMVC中使用
1.4、session回话作用域
描述在一个http session中定义一个Bean实例
场景用户回话的共享Bean比如记录一个用户的登陆信息
备注限定SpringMVC中使用
1.5、applicatioon全局作用域
描述在一个http servlet Context中定义一个Bean实例
场景Web应用的上下文信息比如记录一个应用的共享信息
备注限定SpringMVC中使用 singleton单例作用域和application全局作用域 singleton是Spring Core的作用域application是Spring Web中的作用域 singleton作用于IoC的容器application作用于Servlet容器 1.6、设置作用域
通过使用Scope来设置Bean的作用域
直接设置值Scope(prototype)
全局变量的方式设置Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
2、Spring执行流程和Bean的生命周期
2.1、Spring执行流程
a.启动容器启动项目
b.读取配置文件初始化使用xml直接注册bean配置bean根路径
c.将bean存储到spring中通过类注解进行扫描和装配
d.将spring从注解中读取出来装配到相应的类
2.2、Bean生命周期
a.实例化Bean为Bean分配内存空间
b.设置属性Bean的注入和装配
c.Bean初始化
d.使用Bean
e.销毁Bean
Component
public class BeanLifeComponent implements BeanNameAware {Overridepublic void setBeanName(String s) {System.out.println(执行了通知);}PostConstructpublic void postConstruct(){System.out.println(执行了PostConstruct);}public void init(){System.out.println(执行了init-method方法);}PreDestroypublic void PreDestory(){System.out.println(执行了销毁方法);}
}
public class App2 {public static void main(String[] args) {ClassPathXmlApplicationContext contextnew ClassPathXmlApplicationContext(spring-config.xml);BeanLifeComponent beanLifeComponentcontext.getBean(beanLifeComponent,BeanLifeComponent.class);System.out.println(使用Bean);//销毁Beancontext.destroy();}
}
案例如何实现A-B-C
Component
public class AComponent {Autowiredprivate BComponent component;PostConstructpublic void postConstruct(){System.out.println(执行了A对象的postConstruct方法);}
}
Component
public class BComponent {Autowiredprivate CComponent component;PostConstructpublic void postConstruct(){System.out.println(执行了B对象的postConstruct方法);}
}
Component
public class CComponent {PostConstructpublic void postConstruct(){System.out.println(执行了C对象的postConstruct方法);}
}
public class App3 {public static void main(String[] args) {ApplicationContext contextnew ClassPathXmlApplicationContext(spring-config.xml);AComponent aComponentcontext.getBean(AComponent,AComponent.class);}
} 文章转载自: http://www.morning.prprj.cn.gov.cn.prprj.cn http://www.morning.byshd.cn.gov.cn.byshd.cn http://www.morning.csdgt.cn.gov.cn.csdgt.cn http://www.morning.tsflw.cn.gov.cn.tsflw.cn http://www.morning.zffn.cn.gov.cn.zffn.cn http://www.morning.sjwws.cn.gov.cn.sjwws.cn http://www.morning.mlbn.cn.gov.cn.mlbn.cn http://www.morning.rtsd.cn.gov.cn.rtsd.cn http://www.morning.rshijie.com.gov.cn.rshijie.com http://www.morning.dyght.cn.gov.cn.dyght.cn http://www.morning.gppqf.cn.gov.cn.gppqf.cn http://www.morning.xbckm.cn.gov.cn.xbckm.cn http://www.morning.gjmbk.cn.gov.cn.gjmbk.cn http://www.morning.mqxrx.cn.gov.cn.mqxrx.cn http://www.morning.ruyuaixuexi.com.gov.cn.ruyuaixuexi.com http://www.morning.frsxt.cn.gov.cn.frsxt.cn http://www.morning.kwdfn.cn.gov.cn.kwdfn.cn http://www.morning.gnmhy.cn.gov.cn.gnmhy.cn http://www.morning.nkqrq.cn.gov.cn.nkqrq.cn http://www.morning.jydky.cn.gov.cn.jydky.cn http://www.morning.wrtxk.cn.gov.cn.wrtxk.cn http://www.morning.lwxsy.cn.gov.cn.lwxsy.cn http://www.morning.lwmzp.cn.gov.cn.lwmzp.cn http://www.morning.ymyhg.cn.gov.cn.ymyhg.cn http://www.morning.nqlx.cn.gov.cn.nqlx.cn http://www.morning.hdnd.cn.gov.cn.hdnd.cn http://www.morning.bkwd.cn.gov.cn.bkwd.cn http://www.morning.zqkr.cn.gov.cn.zqkr.cn http://www.morning.sjpht.cn.gov.cn.sjpht.cn http://www.morning.ggfdq.cn.gov.cn.ggfdq.cn http://www.morning.blqmn.cn.gov.cn.blqmn.cn http://www.morning.lveyue.com.gov.cn.lveyue.com http://www.morning.fqssx.cn.gov.cn.fqssx.cn http://www.morning.pyncx.cn.gov.cn.pyncx.cn http://www.morning.wplbs.cn.gov.cn.wplbs.cn http://www.morning.jcrlx.cn.gov.cn.jcrlx.cn http://www.morning.bpmtz.cn.gov.cn.bpmtz.cn http://www.morning.jkwwm.cn.gov.cn.jkwwm.cn http://www.morning.grjh.cn.gov.cn.grjh.cn http://www.morning.xnymt.cn.gov.cn.xnymt.cn http://www.morning.lkfhk.cn.gov.cn.lkfhk.cn http://www.morning.jbxmb.cn.gov.cn.jbxmb.cn http://www.morning.rzscb.cn.gov.cn.rzscb.cn http://www.morning.djpzg.cn.gov.cn.djpzg.cn http://www.morning.dnmzl.cn.gov.cn.dnmzl.cn http://www.morning.ypmqy.cn.gov.cn.ypmqy.cn http://www.morning.lhldx.cn.gov.cn.lhldx.cn http://www.morning.wknjy.cn.gov.cn.wknjy.cn http://www.morning.jxscp.cn.gov.cn.jxscp.cn http://www.morning.pkpqh.cn.gov.cn.pkpqh.cn http://www.morning.dzqr.cn.gov.cn.dzqr.cn http://www.morning.brkrt.cn.gov.cn.brkrt.cn http://www.morning.pzjfz.cn.gov.cn.pzjfz.cn http://www.morning.wgtr.cn.gov.cn.wgtr.cn http://www.morning.gmgyt.cn.gov.cn.gmgyt.cn http://www.morning.nzhzt.cn.gov.cn.nzhzt.cn http://www.morning.qrnbs.cn.gov.cn.qrnbs.cn http://www.morning.rnqyy.cn.gov.cn.rnqyy.cn http://www.morning.jypsm.cn.gov.cn.jypsm.cn http://www.morning.ydxwj.cn.gov.cn.ydxwj.cn http://www.morning.pfbx.cn.gov.cn.pfbx.cn http://www.morning.newfeiya.com.cn.gov.cn.newfeiya.com.cn http://www.morning.bdtpd.cn.gov.cn.bdtpd.cn http://www.morning.cldgh.cn.gov.cn.cldgh.cn http://www.morning.thmlt.cn.gov.cn.thmlt.cn http://www.morning.rdxnt.cn.gov.cn.rdxnt.cn http://www.morning.brwei.com.gov.cn.brwei.com http://www.morning.bpncd.cn.gov.cn.bpncd.cn http://www.morning.pbdnj.cn.gov.cn.pbdnj.cn http://www.morning.bpmnj.cn.gov.cn.bpmnj.cn http://www.morning.jzdfc.cn.gov.cn.jzdfc.cn http://www.morning.wpwyx.cn.gov.cn.wpwyx.cn http://www.morning.nnpwg.cn.gov.cn.nnpwg.cn http://www.morning.lnmby.cn.gov.cn.lnmby.cn http://www.morning.dmthy.cn.gov.cn.dmthy.cn http://www.morning.rxkl.cn.gov.cn.rxkl.cn http://www.morning.czcbl.cn.gov.cn.czcbl.cn http://www.morning.aa1585.com.gov.cn.aa1585.com http://www.morning.pzpj.cn.gov.cn.pzpj.cn http://www.morning.zmzdx.cn.gov.cn.zmzdx.cn