- 第一层(启动类的配置方法)
- 第二层(配置文件 app.properties)
- 第三层(H2 数据库 数据)
- 第四层(配置文件 pom.xml)
- 第五层 (最终执行结果)
第一层(启动类的配置方法)①②③④
所在包 com.softcits.autotasknew - ①②③
① AutoTaskApplication.java :带有mian方法的执行文件
//所在包autoTaskNew - src/mai/java - com.softcits.autotasknew
//AutoTaskApplication.java
//注意添加 AutoTaskConfig.class , DBConfig.class
package com.softcits.autotasknew;
import java.util.List;
import java.util.Map;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import com.softcits.autotasknew.service.AutoTaskNewService;
public class AutoTaskApplication {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(AutoTaskConfig.class,
DBConfig.class);
AutoTaskNewService atns = ctx.getBean(AutoTaskNewService.class);
atns.insertData("mayun");
atns.getDesiredData();
ctx.close();
}
}
② AutoTaskConfig.java
//工程刚建立的时候@Configuration是无法添加的,会显示报错
//必须要先配置号pom.xml文件,才可以添加@Configuration,报错消失
package com.softcits.autotasknew;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AutoTaskConfig {
}
③ DBConfig.java
package com.softcits.autotasknew;
import java.sql.SQLException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import javax.sql.DataSource;
import org.springframework.aop.Advisor;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource;
import org.springframework.transaction.interceptor.RollbackRuleAttribute;
import org.springframework.transaction.interceptor.RuleBasedTransactionAttribute;
import org.springframework.transaction.interceptor.TransactionAttribute;
import org.springframework.transaction.interceptor.TransactionInterceptor;
import com.alibaba.druid.pool.DruidDataSource;
@Configuration
@ComponentScan("com.softcits.autotasknew")
@PropertySource("classpath:/app.properties")
//@EnableAspectJAutoProxy
//@EnableTransactionManagement
public class DBConfig {
@Value("${spring.datasource.url}")
private String dbUrl;
@Value("${spring.datasource.username}")
private String username;
@Value("${spring.datasource.password}")
private String password;
@Value("${spring.datasource.driverClassName}")
private