Spring基础知识
Posted wnlsq
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring基础知识相关的知识,希望对你有一定的参考价值。
spring简介
Spring是一个开放源代码的设计层面框架,他解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用。Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson创建。简单来说,Spring是一个分层的JavaSE/EE full-stack(一站式) 轻量级开源框架。
Spring与解耦合
Spring根据功能的不同,将代码划分为两类:主业务逻辑与系统级服务(交叉业务逻辑)。主业务逻辑间,及主业务逻辑与系统级服务间的耦合度是较高的。Spring对于前述两种较高的耦合度,采用了两种不同的技术进行解耦。使用IoC解耦主业务逻辑间的耦合度,使用AOP解耦主业务逻辑与系统级服务间的耦合度。
spring框架的下载
官网: http://spring.io
spring程序
package com.kaikeba.beans;
public class Student {
private String sname;
private int age;
private Teacher teacher;
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
package com.kaikeba.beans;
public class Teacher {
private String tname;
public String getTname() {
return tname;
}
public void setTname(String tname) {
this.tname = tname;
}
}
package com.kaikeba.util; public class BeanDefined { private String beanId; private String classPath; public String getBeanId() { return beanId; } public void setBeanId(String beanId) { this.beanId = beanId; } public String getClassPath() { return classPath; } public void setClassPath(String classPath) { this.classPath = classPath; } }
package com.kaikeba.util; import java.util.List; public class BeanFactory { private List<BeanDefined> beanDefinedList; public List<BeanDefined> getBeanDefinedList() { return beanDefinedList; } public void setBeanDefinedList(List<BeanDefined> beanDefinedList) { this.beanDefinedList = beanDefinedList; } public Object getBean(String beanId) throws Exception{ Object instance; for(BeanDefined beanObj:beanDefinedList){ if(beanId.equals(beanObj.getBeanId())){ String classPath = beanObj.getClassPath(); Class classFile= Class.forName(classPath); //在默认情况下,Spring工厂是通过调用当前类默认工作方法创建实例对象 instance= classFile.newInstance(); return instance; } } return null; } }
package com.kaikeba.test; import java.util.ArrayList; import java.util.List; import com.kaikeba.beans.Teacher; import com.kaikeba.util.BeanDefined; import com.kaikeba.util.BeanFactory; public class TestMain { public static void main(String[] args) throws Exception { //1.声明注册bean BeanDefined beanObj = new BeanDefined(); beanObj.setBeanId("teacher"); beanObj.setClassPath("com.kaikeba.beans.Teacher"); List beanList = new ArrayList(); beanList.add(beanObj);//spring核心配置 //2.声明一个Spring提供BeanFacotory BeanFactory factory = new BeanFactory(); factory.setBeanDefinedList(beanList); //3.开发人员向BeanFactory索要实例对象. Teacher t= (Teacher) factory.getBean("teacher"); System.out.println(t); } }
以上是关于Spring基础知识的主要内容,如果未能解决你的问题,请参考以下文章
初识Spring源码 -- doResolveDependency | findAutowireCandidates | @Order@Priority调用排序 | @Autowired注入(代码片段
Spring boot:thymeleaf 没有正确渲染片段
What's the difference between @Component, @Repository & @Service annotations in Spring?(代码片段
Spring Rest 文档。片段生成时 UTF-8 中间字节无效 [重复]
解决spring-boot启动中碰到的问题:Cannot determine embedded database driver class for database type NONE(转)(代码片段