秦皇岛陵县网站建设,WordPress去掉你的位置,网站建设费属于广告费用吗,建网站电话Spring基础 - Spring简单例子引入Spring要点
设计一个Spring的Hello World
设计一个查询用户的案例的两个需求#xff0c;来看Spring框架帮我们简化了什么开发工作
pom依赖
?xml version1.0 encodingUTF-8?
project xmlnshtt…Spring基础 - Spring简单例子引入Spring要点
设计一个Spring的Hello World
设计一个查询用户的案例的两个需求来看Spring框架帮我们简化了什么开发工作
pom依赖
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdtech.pdai/groupIdartifactIdspring_framework_demo_hellowrold_xml/artifactIdversion1.0-SNAPSHOT/versionpropertiesmaven.compiler.source8/maven.compiler.sourcemaven.compiler.target8/maven.compiler.targetspring.version5.3.9/spring.versionaspectjweaver.version1.9.6/aspectjweaver.version/propertiesdependenciesdependencygroupIdorg.springframework/groupIdartifactIdspring-context/artifactIdversion${spring.version}/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-core/artifactIdversion${spring.version}/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-beans/artifactIdversion${spring.version}/version/dependencydependencygroupIdorg.aspectj/groupIdartifactIdaspectjweaver/artifactIdversion${aspectjweaver.version}/version/dependency/dependencies/project
工程整体框架如下 App
package tech.pdai.springframework;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import tech.pdai.springframework.entity.User;
import tech.pdai.springframework.service.UserServiceImpl;import java.util.List;/*** author pdai*/
public class App {/*** main interfaces.** param args args*/public static void main(String[] args) {// create and configure beansApplicationContext context new ClassPathXmlApplicationContext(aspects.xml, daos.xml, services.xml);// retrieve configured instance// 将原有的Bean的创建工作转给框架 需要用时从Bean的容器中获取即可简化开发工作UserServiceImpl service context.getBean(userService, UserServiceImpl.class);// use configured instanceListUser userList service.findUserList();// print info from beans
// userList.forEach(a - System.out.println(a.getName() , a.getAge()));}
}
有了Spring框架可以将原有Bean的创建工作转给框架, 需要用时从Bean的容器中获取即可这样便简化了开发工作
总结
Spring框架管理这些Bean的创建工作用户管理Bean转变为框架管理Bean,这个叫做控制翻转 IOC Inversion Of ControlSpring框架托管创建的Bean放在IOC ContainerSpring框架为了更好的让用户配置Bean必然会引入不同方式来配置Bean这便是xml配置 Java配置 注解配置Spring框架既然接管了bean的生成必然需要管理整个Bean的生命周期应用程序代码从IOC Container中获取依赖的bean注入到引用程序中这个过程称之为依赖注入控制翻转是通过依赖注入实现的依赖注入有哪些方式AutoWired Resource Qualifier 同时Bean之间存在依赖存在先后顺序问题以及循环依赖问题
面向切面 - AOP
有了Spring框架通过Aspect注解 定义切面切面中定义拦截所有service的方法 并记录日志框架将日志记录和业务需求的代码解耦了 不再是侵入式了
package tech.pdai.springframework.aspect;import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;import java.lang.reflect.Method;/*** author pdai 拦截所以service方法*/
Aspect
public class LogAspect {/*** aspect for every methods under service package.*/Around(execution(* tech.pdai.springframework.service.*.*(..)))public Object businessService(ProceedingJoinPoint pjp) throws Throwable {// get attribute through annotationMethod method ((MethodSignature) pjp.getSignature()).getMethod();System.out.println(execute method: method.getName());// continue to processreturn pjp.proceed();}}
Spring框架通过定义切面通过拦截切点实现不同业务模块的解耦这个叫做面向切面编程AOP如何实现AOP:代理技术分为静态代理和动态代理动态代理包含JDK代理和CGLIB代理
Spring框架设计如何逐步简化开发的
注解配置方式改造
BeanConfig 不再需要Java配置
package tech.pdai.springframework.config;import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScans;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;/*** author pdai*/
Configuration
EnableAspectJAutoProxy
public class BeansConfig {}
UserDaoImpl 增加了 Repository注解
/*** author pdai*/
Repository
public class UserDaoImpl {/*** mocked to find user list.** return user list*/public ListUser findUserList() {return Collections.singletonList(new User(pdai, 18));}
}
UserServiceImpl 增加了Service 注解并通过Autowired注入userDao. package tech.pdai.springframework.service;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tech.pdai.springframework.dao.UserDaoImpl;
import tech.pdai.springframework.entity.User;/*** author pdai*/
Service
public class UserServiceImpl {/*** user dao impl.*/Autowiredprivate UserDaoImpl userDao;/*** find user list.** return user list*/public ListUser findUserList() {return userDao.findUserList();}}
在App中扫描tech.pdai.springframework包
package tech.pdai.springframework;import java.util.List;import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import tech.pdai.springframework.entity.User;
import tech.pdai.springframework.service.UserServiceImpl;/*** author pdai*/
public class App {/*** main interfaces.** param args args*/public static void main(String[] args) {// create and configure beansAnnotationConfigApplicationContext context new AnnotationConfigApplicationContext(tech.pdai.springframework);// retrieve configured instanceUserServiceImpl service context.getBean(UserServiceImpl.class);// use configured instanceListUser userList service.findUserList();// print info from beansuserList.forEach(a - System.out.println(a.getName() , a.getAge()));}
}
SpringBoot托管配置
Springboot实际上通过约定大于配置的方式使用xx-starter统一的对Bean进行默认初始化用户只需要很少的配置就可以进行开发了。