当前位置: 首页 > news >正文

烟台企业网站制作公司设计网页的心得体会与总结

烟台企业网站制作公司,设计网页的心得体会与总结,html5农业网站模板,蓝杉网站建设公司目录 一 第三方资源配置管理1 管理DataSource连接池对象问题导入1.1 管理Druid连接池1.2 管理c3p0连接池 2 加载properties属性文件问题导入2.1 基本用法2.2 配置不加载系统属性2.3 加载properties文件写法 二 Spring容器1 Spring核心容器介绍问题导入1.1 创建容器1.2 获取bean… 目录 一 第三方资源配置管理1 管理DataSource连接池对象问题导入1.1 管理Druid连接池1.2 管理c3p0连接池 2 加载properties属性文件问题导入2.1 基本用法2.2 配置不加载系统属性2.3 加载properties文件写法 二 Spring容器1 Spring核心容器介绍问题导入1.1 创建容器1.2 获取bean对象1.3 容器类层次结构1.4 BeanFactory 2 Spring核心容器总结2.1 容器相关2.2 bean相关2.3 依赖注入相关 三 Spring注解开发1 注解开发定义Bean对象问题导入1.1 基本使用1.2 Component三个衍生注解 2 纯注解开发模式问题导入2.1 纯注解开发模式介绍2.2 代码演示 3 注解开发Bean作用范围和生命周期管理问题导入3.1 bean作用范围注解配置3.2 bean生命周期注解配置 4 注解开发依赖注入问题导入4.1 使用Autowired注解开启自动装配模式按类型4.2 使用Qualifier注解指定要装配的bean名称4.3 使用Value实现简单类型注入 5 注解开发管理第三方Bean问题导入【第一步】单独定义配置类【第二步】将独立的配置类加入核心配置方式1Import注解导入式方式2ComponentScan扫描式 6 注解开发为第三方Bean注入资源问题导入6.1 简单类型依赖注入6.2 引用类型依赖注入 7 注解开发总结 四 Spring整合其他技术1 Spring整合mybatis1.1 思路分析问题导入1.1.1 MyBatis程序核心对象分析1.1.2 整合MyBatis 1.2 代码实现问题导入【前置工作】【第一步】导入Spring整合Mybatis依赖【第二步】创建JdbcConfig配置DataSource数据源【第三步】创建MybatisConfig整合mybatis【第四步】创建SpringConfig主配置类进行包扫描和加载其他配置类【第五步】定义测试类进行测试 2 Spring整合Junit单元测试问题导入【第一步】导入整合的依赖坐标spring-test【第二步】使用Spring整合Junit专用的类加载器【第三步】加载配置文件或者配置类 一 第三方资源配置管理 说明以管理DataSource连接池对象为例讲解第三方资源配置管理 1 管理DataSource连接池对象 问题导入 配置数据库连接参数时注入驱动类名是用driverClassName还是driver 1.1 管理Druid连接池 数据库准备 create database if not exists spring_db character set utf8; use spring_db; create table if not exists tbl_account(id int primary key auto_increment,name varchar(20),money double ); insert into tbl_account values(null,Tom,1000); insert into tbl_account values(null,Jerry,1000);【第一步】添加Druid连接池依赖 dependencygroupIdcom.alibaba/groupIdartifactIddruid/artifactIdversion1.2.8/version /dependency dependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdversion8.0.20/version /dependency注意除了添加以上两个依赖之外别忘了添加spring-context依赖。 【第二步】配置DruidDataSource连接池Bean对象 bean iddataSource classcom.alibaba.druid.pool.DruidDataSourceproperty namedriverClassName valuecom.mysql.cj.jdbc.Driver/property nameurl valuejdbc:mysql://localhost:3306/spring_db?characterEncodingutf8amp;useSSLfalseamp;serverTimezoneAsia/Shanghaiamp;rewriteBatchedStatementstrue/property nameusername valueroot/property namepassword valueroot/ /bean【第三步】在测试类中从IOC容器中获取连接池对象并打印 public class App {public static void main(String[] args) {ApplicationContext ctx new ClassPathXmlApplicationContext(applicationContext.xml);DataSource dataSource (DataSource) ctx.getBean(dataSource);System.out.println(dataSource);} }打印结果 1.2 管理c3p0连接池 【第一步】添加c3p0连接池依赖 dependencygroupIdc3p0/groupIdartifactIdc3p0/artifactIdversion0.9.1.2/version /dependency【第二步】配置c3p0连接池Bean对象 bean iddataSource classcom.mchange.v2.c3p0.ComboPooledDataSourceproperty namedriverClass valuecom.mysql.cj.jdbc.Driver/property namejdbcUrl valuejdbc:mysql://localhost:3306/spring_db?characterEncodingutf8amp;useSSLfalseamp;serverTimezoneAsia/Shanghaiamp;rewriteBatchedStatementstrue/property nameuser valueroot/property namepassword valueroot//bean注意同一个Spring容器中不能有两个iddataSource的连接池。 【第三步】在测试类中从IOC容器中获取连接池对象并打印 public class App {public static void main(String[] args) {ApplicationContext ctx new ClassPathXmlApplicationContext(applicationContext.xml);DataSource dataSource (DataSource) ctx.getBean(dataSource);System.out.println(dataSource);} }运行结果 com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement - 3, acquireRetryAttempts - 30, acquireRetryDelay - 1000, autoCommitOnClose - false, automaticTestTable - null, breakAfterAcquireFailure - false, checkoutTimeout - 0, connectionCustomizerClassName - null, connectionTesterClassName - com.mchange.v2.c3p0.impl.DefaultConnectionTester, dataSourceName - 1hge0yzax1fmo0yo1qse2m7|4f7d0008, debugUnreturnedConnectionStackTraces - false, description - null, driverClass - com.mysql.cj.jdbc.Driver, factoryClassLocation - null, forceIgnoreUnresolvedTransactions - false, identityToken - 1hge0yzax1fmo0yo1qse2m7|4f7d0008, idleConnectionTestPeriod - 0, initialPoolSize - 3, jdbcUrl - jdbc:mysql://localhost:3306/spring_db?characterEncodingutf8useSSLfalseserverTimezoneAsia/ShanghairewriteBatchedStatementstrue, maxAdministrativeTaskTime - 0, maxConnectionAge - 0, maxIdleTime - 0, maxIdleTimeExcessConnections - 0, maxPoolSize - 15, maxStatements - 0, maxStatementsPerConnection - 0, minPoolSize - 3, numHelperThreads - 3, numThreadsAwaitingCheckoutDefaultUser - 0, preferredTestQuery - null, properties - {user******, password******}, propertyCycle - 0, testConnectionOnCheckin - false, testConnectionOnCheckout - false, unreturnedConnectionTimeout - 0, usesTraditionalReflectiveProxies - false ]2 加载properties属性文件 目的将数据库的连接参数抽取到一个单独的文件中与Spring配置文件解耦。 问题导入 问题1如何解决使用EL表达式读取属性文件中的值结果读取到了系统属性问题 问题2加载properties文件写法标准写法该怎么写 2.1 基本用法 【第一步】编写jdbc.properties属性文件 jdbc.drivercom.mysql.cj.jdbc.Driver jdbc.urljdbc:mysql://localhost:3306/spring_db?characterEncodingutf8useSSLfalseserverTimezoneAsia/ShanghairewriteBatchedStatementstrue jdbc.usernameroot jdbc.passwordroot【第二步】在applicationContext.xml中开启开启context命名空间加载jdbc.properties属性文件 小技巧如果同学们觉得上述复制粘贴方式不好改或者容易改错其实idea是有提示功能的注意不要选错就行了。有些版本的idea没有这个提示那么就按照上面复制粘贴的方式改改完之后可以做成live template模板后期直接用。 context:property-placeholder locationjdbc.properties/【第三步】在配置连接池Bean的地方使用EL表达式获取jdbc.properties属性文件中的值 bean classcom.alibaba.druid.pool.DruidDataSourceproperty namedriverClassName value${jdbc.driver}/property nameurl value${jdbc.url}/property nameusername value${jdbc.username}/property namepassword value${jdbc.password}/ /bean配置完成之后运行之前的获取Druid连接池代码可以获取到连接池对象就表示配置成功。 2.2 配置不加载系统属性 问题 如果属性文件中配置的不是jdbc.username而是usernameroot666那么使用${username}获取到的不是root666而是计算机的名称。 原因 系统属性的优先级比我们属性文件中的高替换了我们的usernameroot666。 解决 解决1换一个名称例如不叫username叫jdbc.username。 解决2使用system-properties-modeNEVER属性表示不使用系统属性。 context:property-placeholder locationjdbc.properties system-properties-modeNEVER/2.3 加载properties文件写法 不加载系统属性 context:property-placeholder locationjdbc.properties system-properties-modeNEVER/加载多个properties文件 context:property-placeholder locationjdbc.properties,msg.properties/加载所有properties文件 context:property-placeholder location*.properties/加载properties文件 标准格式 context:property-placeholder locationclasspath:*.properties/从路径或jar包中搜索并加载properties文件 context:property-placeholder locationclasspath*:*.properties/二 Spring容器 1 Spring核心容器介绍 问题导入 问题按照Bean名称获取Bean有什么弊端按照Bean类型获取Bean有什么弊端 1.1 创建容器 方式一类路径加载配置文件 ApplicationContext ctx new ClassPathXmlApplicationContext(applicationContext.xml);方式二文件路径加载配置文件 ApplicationContext ctx new FileSystemXmlApplicationContext(D:\\applicationContext.xml);加载多个配置文件 ApplicationContext ctx new ClassPathXmlApplicationContext(bean1.xml, bean2.xml);1.2 获取bean对象 方式一使用bean名称获取 需要自己强制类型转换 BookDao bookDao (BookDao) ctx.getBean(bookDao);方式二使用bean名称获取并指定类型 推荐使用 BookDao bookDao ctx.getBean(bookDao, BookDao.class);方式三使用bean类型获取 如果IOC容器中同类型的Bean对象有多个此处获取会报错 BookDao bookDao ctx.getBean(BookDao.class);1.3 容器类层次结构 1.4 BeanFactory 类路径加载配置文件 Resource resources new ClassPathResource(applicationContext.xml); BeanFactory bf new XmlBeanFactory(resources); BookDao bookDao bf.getBean(bookDao, BookDao.class); bookDao.save();BeanFactory创建完毕后所有的Bean均为延迟加载也就是说我们调用getBean()方法获取Bean对象时才创建Bean对象并返回给我们 2 Spring核心容器总结 2.1 容器相关 BeanFactory是IoC容器的顶层接口初始化BeanFactory对象时加载的bean延迟加载ApplicationContext接口是Spring容器的核心接口初始化时bean立即加载ApplicationContext接口提供基础的bean操作相关方法通过其他接口扩展其功能ApplicationContext接口常用初始化类 ClassPathXmlApplicationContext(常用)FileSystemXmlApplicationContext 2.2 bean相关 2.3 依赖注入相关 三 Spring注解开发 1 注解开发定义Bean对象 目的xml配置Bean对象有些繁琐使用注解简化Bean对象的定义 问题导入 问题1使用什么标签进行Spring注解包扫描 问题2Component注解和Controller、Service、Repository三个衍生注解有什么区别 1.1 基本使用 【第一步】在applicationContext.xml中开启Spring注解包扫描 ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:contexthttp://www.springframework.org/schema/contextxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd!--扫描com.itheima包及其子包下的类中注解--context:component-scan base-packagecom.itheima/ /beans【第二步】在类上使用Component注解定义Bean。 //Component定义bean Component(bookDao) public class BookDaoImpl implements BookDao {public void save() {System.out.println(book dao save ...);} } Component public class BookServiceImpl implements BookService {private BookDao bookDao;public void setBookDao(BookDao bookDao) {this.bookDao bookDao;}public void save() {System.out.println(book service save ...);bookDao.save();} }补充说明如果Component注解没有使用参数指定Bean的名称那么类名首字母小写就是Bean在IOC容器中的默认名称。例如BookServiceImpl对象在IOC容器中的名称是bookServiceImpl。 【第三步】在测试类中获取Bean对象 public class AppForAnnotation {public static void main(String[] args) {ApplicationContext ctx new ClassPathXmlApplicationContext(applicationContext.xml);BookDao bookDao (BookDao) ctx.getBean(bookDao);System.out.println(bookDao);//按类型获取beanBookService bookService ctx.getBean(BookService.class);System.out.println(bookService);} }注意在测试类中不要调用bookService的save方法因为还没有给BookServiceImpl中的bookDao赋值调用bookService的save方法会出现空指针异常。 运行结果 1.2 Component三个衍生注解 说明加粗的注解为常用注解 Spring提供 Component 注解的三个衍生注解 Controller用于表现层bean定义Service用于业务层bean定义Repository用于数据层bean定义 Repository(bookDao) public class BookDaoImpl implements BookDao { }Service public class BookServiceImpl implements BookService { }2 纯注解开发模式 问题导入 问题1配置类上使用什么注解表示该类是一个配置类 问题2配置类上使用什么注解进行Spring注解包扫描 2.1 纯注解开发模式介绍 Spring3.0开启了纯注解开发模式使用Java类替代配置文件开启了Spring快速开发赛道Java类代替Spring核心配置文件 Configuration注解用于设定当前类为配置类ComponentScan注解用于设定扫描路径此注解只能添加一次多个数据请用数组格式 ComponentScan({com.itheima.service,com.itheima.dao})读取Spring核心配置文件初始化容器对象切换为读取Java配置类初始化容器对象 //加载配置文件初始化容器 ApplicationContext ctx new ClassPathXmlApplicationContext(applicationContext.xml); //加载配置类初始化容器 ApplicationContext ctx new AnnotationConfigApplicationContext(SpringConfig.class);2.2 代码演示 【第一步】定义配置类代替配置文件 //声明当前类为Spring配置类 Configuration //Spring注解扫描相当于context:component-scan base-packagecom.itheima/ ComponentScan(com.itheima) //设置bean扫描路径多个路径书写为字符串数组格式 //ComponentScan({com.itheima.service,com.itheima.dao}) public class SpringConfig { }【第二步】在测试类中加载配置类获取Bean对象并使用 public class AppForAnnotation {public static void main(String[] args) {//AnnotationConfigApplicationContext加载Spring配置类初始化Spring容器ApplicationContext ctx new AnnotationConfigApplicationContext(SpringConfig.class);BookDao bookDao (BookDao) ctx.getBean(bookDao);System.out.println(bookDao);//按类型获取beanBookService bookService ctx.getBean(BookService.class);System.out.println(bookService);} }3 注解开发Bean作用范围和生命周期管理 问题导入 在类上使用什么注解定义Bean的作用范围 3.1 bean作用范围注解配置 使用Scope定义bean作用范围 单例模式Scope(“singleton”)非单例模式Scope(“prototype”) Repository Scope(singleton) public class BookDaoImpl implements BookDao { }3.2 bean生命周期注解配置 使用PostConstruct 设置bean的初始化方法 PreDestroy 设置bean的销毁方法 Repository Scope(singleton) public class BookDaoImpl implements BookDao {public BookDaoImpl() {System.out.println(book dao constructor ...);}PostConstructpublic void init(){System.out.println(book init ...);}PreDestroypublic void destroy(){System.out.println(book destory ...);} }public class AppForAnnotation {public static void main(String[] args) {//AnnotationConfigApplicationContext加载Spring配置类初始化Spring容器AnnotationConfigApplicationContext ctx new AnnotationConfigApplicationContext(SpringConfig.class);BookDao bookDao1 ctx.getBean(BookDao.class);BookDao bookDao2 ctx.getBean(BookDao.class);System.out.println(bookDao1);System.out.println(bookDao2);ctx.close();注意PostConstruct和PreDestroy注解是jdk中提供的注解从jdk9开始jdk中的javax.annotation包被移除了也就是说这两个注解就用不了了可以额外导入一下依赖解决这个问题。 dependencygroupIdjavax.annotation/groupIdartifactIdjavax.annotation-api/artifactIdversion1.3.2/version /dependency4 注解开发依赖注入 问题导入 问题1请描述Autowired注解是如何进行自动装配的 问题2请描述Qualifier注解的作用 4.1 使用Autowired注解开启自动装配模式按类型 Service public class BookServiceImpl implements BookService {//Autowired注入引用类型自动装配模式默认按类型装配Autowiredprivate BookDao bookDao;public void save() {System.out.println(book service save ...);bookDao.save();} }说明不管是使用配置文件还是配置类都必须进行对应的Spring注解包扫描才可以使用。Autowired默认按照类型自动装配如果IOC容器中同类的Bean有多个那么默认按照变量名和Bean的名称匹配建议使用Qualifier注解指定要装配的bean名称 注意自动装配基于反射设计创建对象并暴力反射对应属性为私有属性初始化数据因此无需提供setter方法。 4.2 使用Qualifier注解指定要装配的bean名称 目的解决IOC容器中同类型Bean有多个装配哪一个的问题 Service public class BookServiceImpl implements BookService {//Autowired注入引用类型自动装配模式默认按类型装配Autowired//Qualifier自动装配bean时按bean名称装配Qualifier(bookDao)private BookDao bookDao;public void save() {System.out.println(book service save ...);bookDao.save();} }注意Qualifier注解无法单独使用必须配合Autowired注解使用 4.3 使用Value实现简单类型注入 Repository(bookDao) public class BookDaoImpl implements BookDao {//Value注入简单类型无需提供set方法Value(${name})private String name;public void save() {System.out.println(book dao save ... name);} }以上Value注解中使用${name}从属性文件中读取name值那么就需要在配置类或者配置文件中加载属性文件。 Configuration ComponentScan(com.itheima) //PropertySource加载properties配置文件 PropertySource({classpath:jdbc.properties}) //{}可以省略不写 public class SpringConfig { }注意PropertySource()中加载多文件请使用数组格式配置不允许使用通配符* 5 注解开发管理第三方Bean 问题导入 导入自己定义的配置类有几种方式 【第一步】单独定义配置类 public class JdbcConfig {//Bean表示当前方法的返回值是一个bean对象添加到IOC容器中Beanpublic DataSource dataSource(){DruidDataSource ds new DruidDataSource();ds.setDriverClassName(com.mysql.cj.jdbc.Driver);ds.setUrl(mysql://localhost:3306/spring_db?characterEncodingutf8useSSLfalseserverTimezoneAsia/ShanghairewriteBatchedStatementstrue);ds.setUsername(root);ds.setPassword(root);return ds;} }【第二步】将独立的配置类加入核心配置 方式1Import注解导入式 Configuration ComponentScan(com.itheima) //Import:导入配置信息 Import({JdbcConfig.class}) public class SpringConfig { }方式2ComponentScan扫描式 Configuration ComponentScan({com.itheima.config,com.itheima.service,com.itheima.dao}) //只要com.itheima.config包扫到了就行三个包可以合并写成com.itheima public class SpringConfig { }6 注解开发为第三方Bean注入资源 问题导入 配置类中如何注入简单类型数据如何注入引用类型数据 6.1 简单类型依赖注入 public class JdbcConfig {//1.定义一个方法获得要管理的对象Value(com.mysql.cj.jdbc.Driver)private String driver;Value(jdbc:mysql://localhost:3306/spring_db?characterEncodingutf8useSSLfalseserverTimezoneAsia/ShanghairewriteBatchedStatementstrue)private String url;Value(root)private String userName;Value(root)private String password;//2.Bean表示当前方法的返回值是一个bean对象添加到IOC容器中Beanpublic DataSource dataSource(){DruidDataSource ds new DruidDataSource();ds.setDriverClassName(driver);ds.setUrl(url);ds.setUsername(userName);ds.setPassword(password);return ds;} }说明如果Value()中使用了EL表达式读取properties属性文件中的内容那么就需要加载properties属性文件。 6.2 引用类型依赖注入 //Spring会自动从IOC容器中找到BookDao对象赋值给参数bookDao变量如果没有就会报错。 Bean public DataSource dataSource(BookDao bookDao){System.out.println(bookDao);DruidDataSource ds new DruidDataSource();ds.setDriverClassName(driver);ds.setUrl(url);ds.setUsername(userName);ds.setPassword(password);return ds; }说明引用类型注入只需要为bean定义方法设置形参即可容器会根据类型自动装配对象 7 注解开发总结 四 Spring整合其他技术 1 Spring整合mybatis 1.1 思路分析 问题导入 mybatis进行数据层操作的核心对象是谁 1.1.1 MyBatis程序核心对象分析 1.1.2 整合MyBatis 使用SqlSessionFactoryBean封装SqlSessionFactory需要的环境信息 使用MapperScannerConfigurer加载Dao接口创建代理对象保存到IOC容器中 1.2 代码实现 问题导入 问题1Spring整合mybatis的依赖叫什么 问题2Spring整合mybatis需要管理配置哪两个Bean这两个Bean作用分别是什么 【前置工作】 在pom.xml中添加spring-context、druid、mybatis、mysql-connector-java等基础依赖。准备service和dao层基础代码 public interface AccountService {void save(Account account);void delete(Integer id);void update(Account account);ListAccount findAll();Account findById(Integer id);} Service public class AccountServiceImpl implements AccountService {Autowiredprivate AccountDao accountDao;public void save(Account account) {accountDao.save(account);}public void update(Account account){accountDao.update(account);}public void delete(Integer id) {accountDao.delete(id);}public Account findById(Integer id) {return accountDao.findById(id);}public ListAccount findAll() {return accountDao.findAll();} } public interface AccountDao {Insert(insert into tbl_account(name,money)values(#{name},#{money}))void save(Account account);Delete(delete from tbl_account where id #{id} )void delete(Integer id);Update(update tbl_account set name #{name} , money #{money} where id #{id} )void update(Account account);Select(select * from tbl_account)ListAccount findAll();Select(select * from tbl_account where id #{id} )Account findById(Integer id); }【第一步】导入Spring整合Mybatis依赖 dependenciesdependencygroupIdorg.springframework/groupIdartifactIdspring-context/artifactIdversion5.3.18/version/dependencydependencygroupIdcom.alibaba/groupIdartifactIddruid/artifactIdversion1.2.8/version/dependencydependencygroupIdorg.mybatis/groupIdartifactIdmybatis/artifactIdversion3.5.6/version/dependencydependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdversion8.0.20/version/dependencydependencygroupIdorg.mybatis/groupIdartifactIdmybatis-spring/artifactIdversion2.0.6/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-jdbc/artifactIdversion5.3.18/version/dependency/dependencies【第二步】创建JdbcConfig配置DataSource数据源 jdbc.drivercom.mysql.cj.jdbc.Driver jdbc.urljdbc:mysql://localhost:3306/spring_db?characterEncodingutf8useSSLfalseserverTimezoneAsia/ShanghairewriteBatchedStatementstrue jdbc.usernameroot jdbc.passwordrootpublic class JdbcConfig {Value(${jdbc.driver})private String driver;Value(${jdbc.url})private String url;Value(${jdbc.username})private String userName;Value(${jdbc.password})private String password;Beanpublic DataSource dataSource(){DruidDataSource ds new DruidDataSource();ds.setDriverClassName(driver);ds.setUrl(url);ds.setUsername(userName);ds.setPassword(password);return ds;} }【第三步】创建MybatisConfig整合mybatis public class MybatisConfig {//定义beanSqlSessionFactoryBean用于产生SqlSessionFactory对象Beanpublic SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){SqlSessionFactoryBean ssfb new SqlSessionFactoryBean();ssfb.setTypeAliasesPackage(com.itheima.domain);ssfb.setDataSource(dataSource);return ssfb;}//定义bean返回MapperScannerConfigurer对象Beanpublic MapperScannerConfigurer mapperScannerConfigurer(){MapperScannerConfigurer msc new MapperScannerConfigurer();msc.setBasePackage(com.itheima.dao);return msc;} }【第四步】创建SpringConfig主配置类进行包扫描和加载其他配置类 Configuration ComponentScan(com.itheima) //PropertySource加载类路径jdbc.properties文件 PropertySource(classpath:jdbc.properties) Import({JdbcConfig.class,MybatisConfig.class}) public class SpringConfig { }【第五步】定义测试类进行测试 public class App {public static void main(String[] args) {ApplicationContext ctx new AnnotationConfigApplicationContext(SpringConfig.class);AccountService accountService ctx.getBean(AccountService.class);Account ac accountService.findById(1);System.out.println(ac);} }2 Spring整合Junit单元测试 问题导入 Spring整合Junit的两个注解作用分别是什么 【第一步】导入整合的依赖坐标spring-test dependenciesdependencygroupIdorg.springframework/groupIdartifactIdspring-context/artifactIdversion5.3.18/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-jdbc/artifactIdversion5.3.18/version/dependency!--spring整合junit--dependencygroupIdorg.springframework/groupIdartifactIdspring-test/artifactIdversion5.3.18/versionscopetest/scope/dependencydependencygroupIdcom.alibaba/groupIdartifactIddruid/artifactIdversion1.2.8/version/dependencydependencygroupIdorg.mybatis/groupIdartifactIdmybatis/artifactIdversion3.5.6/version/dependencydependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdversion8.0.20/version/dependencydependencygroupIdorg.mybatis/groupIdartifactIdmybatis-spring/artifactIdversion2.0.6/version/dependency!--junit--dependencygroupIdjunit/groupIdartifactIdjunit/artifactIdversion4.13.2/versionscopetest/scope/dependency/dependencies【第二步】使用Spring整合Junit专用的类加载器 【第三步】加载配置文件或者配置类 //【第二步】使用Spring整合Junit专用的类加载器 RunWith(SpringJUnit4ClassRunner.class) //【第三步】加载配置文件或者配置类 ContextConfiguration(classes {SpringConfiguration.class}) //加载配置类 //ContextConfiguration(locations{classpath:applicationContext.xml})//加载配置文件 public class AccountServiceTest {//支持自动装配注入beanAutowiredprivate AccountService accountService;Testpublic void testFindById(){System.out.println(accountService.findById(1));}Testpublic void testFindAll(){System.out.println(accountService.findAll());} }注意junit的依赖至少要是4.12版本,可以是4.13等版本,否则出现如下异常
文章转载自:
http://www.morning.tsdqr.cn.gov.cn.tsdqr.cn
http://www.morning.ygztf.cn.gov.cn.ygztf.cn
http://www.morning.msfqt.cn.gov.cn.msfqt.cn
http://www.morning.yprnp.cn.gov.cn.yprnp.cn
http://www.morning.gyrdn.cn.gov.cn.gyrdn.cn
http://www.morning.rkkpr.cn.gov.cn.rkkpr.cn
http://www.morning.mtbth.cn.gov.cn.mtbth.cn
http://www.morning.fnjrh.cn.gov.cn.fnjrh.cn
http://www.morning.dtrcl.cn.gov.cn.dtrcl.cn
http://www.morning.kdbbm.cn.gov.cn.kdbbm.cn
http://www.morning.zwhtr.cn.gov.cn.zwhtr.cn
http://www.morning.rlxnc.cn.gov.cn.rlxnc.cn
http://www.morning.mlnzx.cn.gov.cn.mlnzx.cn
http://www.morning.xywfz.cn.gov.cn.xywfz.cn
http://www.morning.tgfsr.cn.gov.cn.tgfsr.cn
http://www.morning.mingjiangds.com.gov.cn.mingjiangds.com
http://www.morning.kkgbs.cn.gov.cn.kkgbs.cn
http://www.morning.pzbqm.cn.gov.cn.pzbqm.cn
http://www.morning.hpprx.cn.gov.cn.hpprx.cn
http://www.morning.yrngx.cn.gov.cn.yrngx.cn
http://www.morning.bqdgr.cn.gov.cn.bqdgr.cn
http://www.morning.080203.cn.gov.cn.080203.cn
http://www.morning.fpbj.cn.gov.cn.fpbj.cn
http://www.morning.gbfuy28.cn.gov.cn.gbfuy28.cn
http://www.morning.xnltz.cn.gov.cn.xnltz.cn
http://www.morning.jjsxh.cn.gov.cn.jjsxh.cn
http://www.morning.knscf.cn.gov.cn.knscf.cn
http://www.morning.yrsg.cn.gov.cn.yrsg.cn
http://www.morning.lhrxq.cn.gov.cn.lhrxq.cn
http://www.morning.tdldh.cn.gov.cn.tdldh.cn
http://www.morning.bsbcp.cn.gov.cn.bsbcp.cn
http://www.morning.yfqhc.cn.gov.cn.yfqhc.cn
http://www.morning.qsbcg.cn.gov.cn.qsbcg.cn
http://www.morning.jkcnq.cn.gov.cn.jkcnq.cn
http://www.morning.ycgrl.cn.gov.cn.ycgrl.cn
http://www.morning.pgxjl.cn.gov.cn.pgxjl.cn
http://www.morning.nmpdm.cn.gov.cn.nmpdm.cn
http://www.morning.rggky.cn.gov.cn.rggky.cn
http://www.morning.nqmwk.cn.gov.cn.nqmwk.cn
http://www.morning.njhyk.cn.gov.cn.njhyk.cn
http://www.morning.jxmjr.cn.gov.cn.jxmjr.cn
http://www.morning.tqrxm.cn.gov.cn.tqrxm.cn
http://www.morning.bojkosvit.com.gov.cn.bojkosvit.com
http://www.morning.fksrg.cn.gov.cn.fksrg.cn
http://www.morning.fkdts.cn.gov.cn.fkdts.cn
http://www.morning.prjty.cn.gov.cn.prjty.cn
http://www.morning.jjwzk.cn.gov.cn.jjwzk.cn
http://www.morning.nkdmd.cn.gov.cn.nkdmd.cn
http://www.morning.rnfn.cn.gov.cn.rnfn.cn
http://www.morning.mqbsm.cn.gov.cn.mqbsm.cn
http://www.morning.lizimc.com.gov.cn.lizimc.com
http://www.morning.pzpj.cn.gov.cn.pzpj.cn
http://www.morning.wbrf.cn.gov.cn.wbrf.cn
http://www.morning.dsxgc.cn.gov.cn.dsxgc.cn
http://www.morning.rdkt.cn.gov.cn.rdkt.cn
http://www.morning.ntwxt.cn.gov.cn.ntwxt.cn
http://www.morning.gwqcr.cn.gov.cn.gwqcr.cn
http://www.morning.zbpqq.cn.gov.cn.zbpqq.cn
http://www.morning.jjmrx.cn.gov.cn.jjmrx.cn
http://www.morning.jsrnf.cn.gov.cn.jsrnf.cn
http://www.morning.qhmhz.cn.gov.cn.qhmhz.cn
http://www.morning.tfrmx.cn.gov.cn.tfrmx.cn
http://www.morning.jsljr.cn.gov.cn.jsljr.cn
http://www.morning.piekr.com.gov.cn.piekr.com
http://www.morning.lbcfj.cn.gov.cn.lbcfj.cn
http://www.morning.nxdqz.cn.gov.cn.nxdqz.cn
http://www.morning.hhqjf.cn.gov.cn.hhqjf.cn
http://www.morning.wgxtz.cn.gov.cn.wgxtz.cn
http://www.morning.xckqs.cn.gov.cn.xckqs.cn
http://www.morning.bwkhp.cn.gov.cn.bwkhp.cn
http://www.morning.pwzzk.cn.gov.cn.pwzzk.cn
http://www.morning.pfmsh.cn.gov.cn.pfmsh.cn
http://www.morning.dxhnm.cn.gov.cn.dxhnm.cn
http://www.morning.mjytr.cn.gov.cn.mjytr.cn
http://www.morning.cgthq.cn.gov.cn.cgthq.cn
http://www.morning.rzcfg.cn.gov.cn.rzcfg.cn
http://www.morning.qgbfx.cn.gov.cn.qgbfx.cn
http://www.morning.mtxrq.cn.gov.cn.mtxrq.cn
http://www.morning.yxgqr.cn.gov.cn.yxgqr.cn
http://www.morning.pznqt.cn.gov.cn.pznqt.cn
http://www.tj-hxxt.cn/news/262763.html

相关文章:

  • 网站建设多少钱一年上海网站建设y021
  • 企业网站 开源广东专业移动网站服务商
  • 如何建立小企业网站怎么联系网站管理员
  • 章贡区网站建设如何开wordpress网站
  • php企业网站建设东山县建设银行网站
  • 个人站长做哪些网站好如何制作网址快捷方式
  • 重庆公司专业建站学做古装网站
  • 建企业网站 硬件萧山区住房和城乡建设局网站
  • 某企业集团网站建设方案网站建设总结 优帮云
  • 烟台网站制作山海云服务器网站目录
  • wordpress手机网站模版品牌手机网站开发
  • 网站建设完不管了自己怎么接手wordpress打字不显示
  • 应聘网站开发的自我介绍十大品牌网站
  • 苏州专业网站设计制作公司南宁哪里有seo推广厂家
  • 网站建设与管理小论文南京专业网站营销
  • 建设网站必须要配置apache吗网站开发维护者
  • 移动网站 案例郑州网站建设与设计
  • 运营网站求个网站你懂我的意思吗
  • 网站建设进度巴中免费网站建设
  • 有没有做网站一次付费权重7以上的网站
  • 中国万网网站空间申请wordpress相关的网站
  • 厦门网页建站申请费用sem竞价推广
  • 大同做网站游戏网站建设计划书
  • 钢材销售都在哪个网站做网站开发主要内容和要求
  • 郴州北京网站建设大屏可视化ui设计
  • 样本之家登录网站天津市住房和城乡建设局网站
  • 网站建设 中企动力烟台沙洋网站定制
  • 网站建设 源美设计网站动态背景怎么做
  • 为公司制作网站线上培训网站开发
  • 新浪云怎么做自己的网站网件路由器r7000