@Bean // 事务管理
public PlatformTransactionManager transactionManager() throws Exception{
return new DataSourceTransactionManager(dataSource());
}
}`
事务类
`import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.itdragon.dao.ITDragonDao;
@Service
public class ITDragonServer {
@Autowired(required=false)
private ITDragonDao itdragonDao;
public List<Map<String,Object>> findAll() {
return itdragonDao.findAll();
}
@Transactional
public void updateNameById(String name, Long id) {
itdragonDao.updateUserNameById(name, id);
System.out.println(0/0); // 事务异常
}
}`
完整代码,请异步github
二、组件注入
------
Bean注解类似xml文件中的`<bean>`标签,其中被Bean注解修饰的方法名对应`<bean>`标签中的id,也可以通过Bean注解的value属性设置id的值。在SpringBoot底层代码中被大量使用。
若希望容器启动后创建对象,并在使用后从容器中直接获取,则什么都不需要做,因为Spring默认是单实例,即容器启动后创建对象,并保存到容器中,使用时再从容器中获取。
若希望容器启动后不创建对象,而是在使用时再创建,继而保存到容器中,下次使用再从容器中获取,可以通过懒加载的方式实现,即使用Lazy注解修饰。
若希望容器启动后不创建对象,而是在每次使用时创建,则采用多实例的方式,即使用Scope注解,参数的值为prototype,[即@Scope](mailto:%E5%8D%B3@Scope)("prototype") 。
若希望容器启动后根据条件选择需要注入的Bean,可以使用注解Conditional判断,SpringBoot的底层打量使用了该注解。
`import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Controller;
import com.itdragon.entity.ITDragonEntity;
import com.itdragon.server.ITDragonServer;
/** * 知识点二:配置自动扫描包路径 * 一、注解ComponentScan的value值设置自动扫描包的路径 * 二、注解ComponentScan的excludeFilters值设置扫描排除的规则 * 1)、通过注解@Filter设置排除的类型,type=ANNOTATION表示按照注解包含排除。classes是具体的注解,如Controller,Server,Repository * 三、注解ComponentScan的includeFilters值设置扫描加入的规则 * 1)、通过设置useDefaultFilters=false关闭Spring默认扫描全部的功能,使includeFilters生效 * * 知识点三:@Filter常用的拦截类型 * 一、FilterType.ANNOTATION:按照注解 * 二、FilterType.ASSIGNABLE_TYPE:按照给定的类型,包括其子类和实现类 * 三、FilterType.CUSTOM:使用自定义规则 * * 第一个ComponentScan注解表示在指定包下不扫描通过Controller注解修饰的类和ITDragonServer类及其子类和实现类 * 第二个ComponentScan注解表示在指定包下只扫描通过Controller注解修饰的类 * 第三个ComponentScan注解表示在指定包下根据自定义拦截规则,不扫描满足规则的类 */
@Configuration
@ComponentScan(value=“com.itdragon”,excludeFilters={@Filter(type=FilterType.ANNOTATION,classes={Controller.class}),
@Filter(type=FilterType.ASSIGNABLE_TYPE,classes={ITDragonServer.class})})
//@ComponentScan(value=“com.itdragon”,includeFilters={@Filter(type=FilterType.ANNOTATION,classes={Controller.class})},useDefaultFilters=false)
//@ComponentScan(value=“com.itdragon”,excludeFilters={@Filter(type=FilterType.CUSTOM,classes={ITDragonCustomTypeFilter.class})})
public class ITDragonConfig {
/** * 知识点一:配置bean * 一、注解Bean的value值表示bean的id * 二、注解Bean的value值未设置,则方法名表示bean的id */
@Bean(value="itdragonBean")
public ITDragonEntity itdragonEntity() { //方法名很重要,类似xml的id名,也可以通过@bean的value值重定义
return new ITDragonEntity("itdragon", "configuration-password", 25);
}
/** * 知识点四:Scope属性 * @Scope,调整作用域,默认单实例 * singleton:单实例:ioc容器启动后创建对象放到ioc容器中,需要是从容器中获取。 * prototype:多实例:ioc容器启动后每次获取对象时都要创建对象。 * request:同一次请求创建一个实例 * session:同一个session创建一个实例 * * 知识点五:懒加载 * 针对单实例而言,在容器启动后不创建对象,在第一次使用Bean时创建对象。可以理解为单实例的一种补充。 * */
// @Scope(“prototype”)
@Lazy
@Bean
public ITDragonEntity scopeTopicBean() {
System.out.println("^^^^^^^^^^^^^^^^^^^^^Create Bean");
return new ITDragonEntity("scopeBean", "singleton-prototype-request-session", 25);
}
/** * 知识点六:Conditional条件判断 * 满足条件才会注册bean,可以修饰在类上,管理整个类下的组件注入。 */
@Bean
@Conditional({ITDragonCustomCondition.class})
public ITDragonEntity conditionalBean() {
return new ITDragonEntity("conditionalBean", "Conditional-Condition-CustomCondition", 25);
}
/** * 知识点七:FactoryBean工厂Bean * FactoryBean默认通过调用getObject创建的对象,通过调用isSingleton设置单实例和多实例。 */
@Bean
public ITDragonFactoryBean itdragonFactoryBean() {
return new ITDragonFactoryBean();
}
}`
自定义的条件判断组件注入类
`import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
/** * 自定义的条件判断组件注入 * @author itdragon * */
public class ITDragonCustomCondition implements Condition{
/** * 判断注册的bean中是否含有指定的bean */
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
// 获取bean的注册类
BeanDefinitionRegistry registry = context.getRegistry();
return registry.containsBeanDefinition("scopeTopicBean"); // 有则加载conditionalBean
}
}`
自定义Bean的工厂类
`import org.springframework.beans.factory.FactoryBean;
import com.itdragon.entity.ITDragonEntity;
/** * 自定义Bean的工厂类 * @author itdragon * */
public class ITDragonFactoryBean implements FactoryBean{
public ITDragonEntity getObject() throws Exception {
System.out.println("^^^^^^^^^^^^^^^^^^^^^FactoryBean Create Bean");
return new ITDragonEntity(); // 创建对象并返回到容器中
}
public Class<?> getObjectType() {
return ITDragonEntity.class;
}
public boolean isSingleton() {
return false; // 设置多实例,true则为单例
}
}`
三、属性赋值
------
属性赋值步骤:
第一步:通过注解PropertySource引入外部文件。可以引入多个,若担心文件不存在,可以通过参数ignoreResourceNotFound设置忽略
第二步:通过注解Value从外部文件中获取值,一般采用${}格式,还支持#{} SpEL表达式,也可以直接传字符串。如果想接收一些复杂的值,比如集合,可以考虑使用注解ConfigurationProperties,后续会详细介绍两者的优缺点。
`import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import com.itdragon.entity.ITDragonEntity;
/** * 知识点一: 引入外部文件,并从文件中获取值 * @PropertySource 引入外部文件,支持多个,如果文件不存在会报错,可以通过设置参数ignoreResourceNotFound=true忽略 * @Value 从外部文件中获取值,支持spel表达式,#{},${},string * @author itdragon */
@Configuration
@PropertySource(value={“classpath:propertySource.properties”,“classpth:xxx.properties”},ignoreResourceNotFound=true)
public class ITDragonConfigValue {
@Value("${ACCOUNT}") // 从配置文件获取数据
private String ACCOUNT;
@Value("${PASSWORD}")
private String PASSWORD;
总结
我们总是喜欢瞻仰大厂的大神们,但实际上大神也不过凡人,与菜鸟程序员相比,也就多花了几分心思,如果你再不努力,差距也只会越来越大。实际上,作为程序员,丰富自己的知识储备,提升自己的知识深度和广度是很有必要的。
Mybatis源码解析
从配置文件获取数据
private String ACCOUNT;
@Value("${PASSWORD}")
private String PASSWORD;
总结
我们总是喜欢瞻仰大厂的大神们,但实际上大神也不过凡人,与菜鸟程序员相比,也就多花了几分心思,如果你再不努力,差距也只会越来越大。实际上,作为程序员,丰富自己的知识储备,提升自己的知识深度和广度是很有必要的。
Mybatis源码解析
[外链图片转存中…(img-hhHcrbVQ-1628675704446)]