青海做网站找谁,wordpress gif缩略图,网站如何做注册类 cpa,wordpress图床首页无缩略图IOC 注解开发
版本了解 2.0版本时开始支持注解开发#xff08;2.0之前就是昨天学习的纯 xml 操作#xff09;2.5版本才完善3.0版本支持纯注解开发
1.注解xml配置Bean
1.1配置用户mapper 配置 mapper public interface UserMapper {void save();
}配置 mapper 实现类…IOC 注解开发
版本了解 2.0版本时开始支持注解开发2.0之前就是昨天学习的纯 xml 操作2.5版本才完善3.0版本支持纯注解开发
1.注解xml配置Bean
1.1配置用户mapper 配置 mapper public interface UserMapper {void save();
}配置 mapper 实现类(还没有交给 spring 管理) public class UserMapperImpl implements UserMapper {public void save() {System.out.println(保存用户成功);}
}1.2将mapper交给spring管理 使用 Component 注解 配置 mapper 实现类 Component
public class UserMapperImpl implements UserMapper {public void save() {System.out.println(保存用户成功);}
}配置 xml 包扫描路径 context:component-scan base-packagecn.sycoder.xmlAnnotationBean.mapper/?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://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.xsdcontext:component-scan base-packagecn.sycoder.xmlAnnotationBean.mapper/
/beans1.3通过容器获取bean 获取 applicationContext ApplicationContext context new ClassPathXmlApplicationContext(xmlAndAnnotation.xml);获取bean Test
public void testComponent(){ApplicationContext context new ClassPathXmlApplicationContext(xmlAndAnnotation.xml);final UserMapper bean context.getBean(UserMapper.class);bean.save();
}1.4Component详解 默认不传参bean 的名称是首字母小写其余不变 正规命名的时候UserMapperImpl --- userMapperImpl
不正规命名时候UUserMapperImpl--- UUserMapperImpl给bean 指定名称 Component(u) 参数就是你bean的名称使用位置具体类的上方不要使用到接口上 作用将bean 交给spring管理 延伸的注解注意和Component一模一样的只不过是用于给程序员区分业务组件的 Controller 用于控制层 Controller
public class UserController {//写接口
}Service 用于业务层 Service
public class UserServiceImpl implements IUserService {public void save() {}
}Repository 用于持久层 Repository
public class UserMapperImpl implements UserMapper {public void save() {System.out.println(保存用户成功);}
}2.纯注解配置Bean
2.1配置学生Mapper mapper 接口 public interface StudentMapper {void save();
}mapper 接口实现类 Repository
public class StudentMapperImpl implements StudentMapper {public void save() {System.out.println(保存学生成功);}
}2.2添加配置类
2.2.1Configuration详解 使用 Configuration Configuration
public class SpringConfig {
}将 SpringConfig 类变成spring 的配置类替换 xml 配置文件 作用标识该类是spring的配置类 配置名称默认首字母小写 使用在类上
2.2.2ComponentScan详解 配置包扫描 ComponentScan Configuration
ComponentScan(cn.sycoder.annotationBean.mapper)
public class SpringConfig {
}作用配置包扫描路径,当前包及其子包都会扫描 value:指定包的路径用于扫描并且注册bean
2.3获取bean 获取 applicationContext ApplicationContext context new AnnotationConfigApplicationContext(SpringConfig.class);获取 bean Testpublic void testAnnotation(){ApplicationContext context new AnnotationConfigApplicationContext(SpringConfig.class);System.out.println(context);final StudentMapper bean context.getBean(StudentMapper.class);System.out.println(bean);}3.注解开发与xml 的梳理 使用Component 替代 xml 的过程梳理 使用 Configuration ComponentScan 与 xml 配置过程的梳理
4.bean scops 配置类 Configuration
ComponentScan({cn.sycoder.scopes})
public class ScopesConfig {
}配置 bean Component
public class ScopeBean {
}获取 bean 执行发现bean 单例的 Testpublic void testScope(){ApplicationContext context new AnnotationConfigApplicationContext(ScopesConfig.class);final ScopeBean bean context.getBean(ScopeBean.class);final ScopeBean bean1 context.getBean(ScopeBean.class);System.out.println(bean);System.out.println(bean1);}4.1通过注解修改 scope Scope Component
Scope(prototype)
public class ScopeBean {
}4.2Scope 详解
位置定义到类上方作用修改对象创建的作用域属性默认是singleton单例的可以修改成 prototype(原型)
5.bean 生命周期常用注解 建立 bean 与使用 Component
public class LifeBean {public LifeBean(){System.out.println(构造器执行了);}PostConstructpublic void init(){System.out.println(初始化bean);}PreDestroypublic void destroy(){System.out.println(销毁bean);}
}5.1PostConstruct详解
位置方法上作用设置该方法为初始化方法
5.2PreDestroy
位置方法上作用设置该方法为销毁方法
5.3注解与 xml 的梳理