昆明找工作哪个网站好,可以做彩页的网站,衣联网和一起做网站 哪家强,国内做轮胎网站哪家好一、Spring框架介绍 Spring优点#xff1a;
1、方便解耦#xff0c;简化开发,IOC控制反转
Spring 就是一个大工厂#xff0c;可以将所有对象创建和依赖关系维护交给Spring
2、AOP 编程的支持
Spring 提供面向切编程#xff0c;可以方便的实现对序进行权限拦截、运监控等…一、Spring框架介绍 Spring优点
1、方便解耦简化开发,IOC控制反转
Spring 就是一个大工厂可以将所有对象创建和依赖关系维护交给Spring
2、AOP 编程的支持
Spring 提供面向切编程可以方便的实现对序进行权限拦截、运监控等功能
3、声明式事务的支持张三给李四转账要么同时成功要么同时失败
只需要通过配置就可以完成对事务的管理而无手动编程
4、方便集成各种优秀框架
Spring 不排斥各种优秀的开源框架其内部提供了对各种优优秀框架的支持如StrutsMybatisHibernate。
SSH SSM
二、IOC和DI 控制反转(Inversion on Control)IOC对象的创建交给外部容器来完成这里就是交给Spring容器这就叫控制反转。 IOC:Spring就是一个大的内存容器一块内存区域三层架构里面上一层不需要再去new下一层对象在上一层只需要写下一层的接口new对象由Spring容器帮我们完成各层直接向Spring容器要对象就可以。 class StudentController{// 需要什么就去创建什么自己去new,这就叫“控制正转”(通俗一点就是自己控制new哪个对象)private IStudentService studentService new StudentServiceImpl();
}class StudentController{// 对象的创建交给别人去new现在交给Spring容器new StudentServiceImpl(),new出来对方放在Spring容器这就叫控制反转“IOC”private IStudentService studentService; // 将Spring容器中new出来的对象通过set方法赋值给studentService这个过程叫依赖注入DIpublic void setStudentService(IStudentService studentService) {this.studentService studentService;}
} 依赖注入Dependency injection DI 现在new这个对象不是由自己new是由Spring容器帮我们new对象现在要得到这个Spring容器new出来的对象就要“依赖”Spring容器“注入”Spring容器new出来的对象。 IOC和DI区别 IOC解决对象创建的问题对象的创建交给别人Inversion of Control。 DI:在创建完对象后对象关系的处理就是依赖注入通过set方法实现依赖注入Dependency injection。 先有IOC对象创建再有DI处理对象关系 在三层架构中最终实现的效果是在Controller不会出现Service层具体的实现类代码只会看到Service层接口Service层也不会出现Dao层具体实现类的代码也只会看到Dao层的接口 四、Bean的属性: scope范围
1、singleton
是scope的默认值单例模式在Spring容器中只存在一个实例。
public void test3() {ApplicationContext context new ClassPathXmlApplicationContext(applicationContext.xml);Student student1 (Student) context.getBean(student);Student student2 (Student) context.getBean(student);System.out.println(student1 student2);// true
}
2、prototype
多例会创建多个对象每次去容器里面拿会创建一个新的实例
bean scopeprototype namestudent classcom.situ.spring.pojo.Student/public void test3() {ApplicationContext context new ClassPathXmlApplicationContext(applicationContext.xml);Student student1 (Student) context.getBean(student);Student student2 (Student) context.getBean(student);System.out.println(student1 student2);// false
}
五、Spring属性注入方式
1、set方法注入
bean namebanji classcom.situ.spring.pojo.Banjiproperty nameid value1/property namename valueJava2023/
/bean
bean namestudent classcom.situ.spring.pojo.Student!-- 值类型注入 --property nameid value1/property namename value张三/property nameage value23/property namegender value男/!-- ref:reference参考、引用引用类型的注入--property namebanji refbanji/
/bean
2、构造方法注入
argument:参数
parameter参数
bean namebanji classcom.situ.spring.pojo.Banjiconstructor-arg nameid value1/constructor-arg namename valueJava2023/
/bean
bean namestudent classcom.situ.spring.pojo.Studentconstructor-arg nameid value1/constructor-arg namename value李四/constructor-arg nameage value23/constructor-arg namegender value男/constructor-arg namebanji refbanji/
/bean
3、注解方式注入
Resource Autowired
六、三层架构使用Spring来管理
1、set方法注入
三层架构使用Spring来管理达到一个目的在上层只看到下一层的接口就可以不需要出现具体的实现类。
使用set方式注入
bean namestudentDao classcom.situ.spring.dao.impl.StudentDaoImpl/
bean namestudentService classcom.situ.spring.service.impl.StudentServiceImplproperty namestudentDao refstudentDao/
/bean
bean namestudentController classcom.situ.spring.controller.StudentControllerproperty namestudentService refstudentService/
/bean
2、注解开发方式
!--base-package:是要扫描的包扫描这个包下面类上带有注解Controller Service Repositioy --
context:component-scan base-packagecom.situ.spring/ Controller Service Repository 本身没有区别都是new一个对象放到Spring容器中 Component new一个对象放到Spring容器中,不起名字放到容器中默认的名字是类名首个单词首字母小写 Target(ElementType.TYPE)
Retention(RetentionPolicy.RUNTIME)
Documented
Component
public interface Controller {/*** The value may indicate a suggestion for a logical component name,* to be turned into a Spring bean in case of an autodetected component.* return the suggested component name, if any (or empty String otherwise)*/AliasFor(annotation Component.class)String value() default ;}
/*
bean namecourseController classcom.situ.spring.controller.CourseController
/bean
Controller 这个注解相当于在applicationContext.xml中写的上面的bean
默认的名字是类名的首个单词小写courseController
*/
Controller(courseController)
public class CourseController {// property namecourseService refcourseService/// Resource从Spring容器中根据名字拿出指定的对象注入进来Resource(name courseService)private ICourseService courseService;public void selectAll() {System.out.println(CourseController.selectAll());courseService.selectAll();}
}/*
bean namecourseService classcom.situ.spring.service.impl.CourseServiceImpl
/bean
*/
Service(courseService)
public class CourseServiceImpl implements ICourseService{//property namecourseDao refcourseDao/Resource(name courseDao)private ICourseDao courseDao;Overridepublic void selectAll() {System.out.println(CourseServiceImpl.selectAll());courseDao.selectAll();}
}// bean namecourseDao classcom.situ.spring.dao.impl.CourseDaoImpl/bean
Repository(courseDao)
public class CourseDaoIml implements ICourseDao{Overridepublic void selectAll() {System.out.println(CourseDaoIml.selectAll());}
} Controller、Service、Repository这三个注解的作用和Component是一样的都是new一个对象放到Spring容器中目的是为了表明三层架构中不同的层。 七、Autowired 自动装配
1、Autowired和Resource区别 Resource默认是按照名称装配的是JDK提供的。 byName 通过参数名自动装配如果一个bean的name 和另外一个bean的 property 相同就自动装配。 Autowired是默认按照类型装配的 是Spring提供的。 byType 通过参数的数据类型自动自动装配如果一个bean的数据类型和另外一个bean的property属性的数据类型兼容就自动装配。 2、Autowired一个接口有多个子类情况
Service
public class BanjiServiceImpl implements IBanjiService {}Service
public class BanjiServiceImpl2 implements IBanjiService{} 这样会报错 expected single matching bean but found 2: banjiServiceImpl,banjiServiceImpl2 因为在容器中子类对象有两个而且变量的名字是banjiService和任何一个容器中对象的名字都不一致所以找不到只有一个子类对象情况下变量名可以随便写 也同时证明Controller、Service、Repository不起名字默认的名字是类的名字首字母变小写。 解决方法 方法一IBanjiService banjiServiceImpl2; 根据变量名去容器中找相同名字的bean对象所以注入过来的是new BanjiServiceImpl2()的对象 方法二还是希望写成IBanjiService banjiServiceQualifier中限定名字 // Resource(name banjiServiceImpl2)
Autowired
Qualifier(value banjiServiceImpl2)
private IBanjiService banjiService; 3、Autowired注入bean Controller
public class StudentController {// property namestudentService refstudentService/// Resource(name studentService)Autowiredprivate IStudentService studentService;}//Service(studentService)
Service
public class StudentServiceImpl implements IStudentService{// Resource(name studentDao)Autowiredprivate IStudentDao studentDao;}//Repository(studentDao)
Repository
public class StudentDaoImpl implements IStudentDao{}