东莞响应式网站价格太原网站建设公司哪家好

张小明 2026/1/10 9:03:32
东莞响应式网站价格,太原网站建设公司哪家好,淘宝入驻网站建设,电影网站如何做seo优化目录 1.快速入门 2.常用注解 bean管理类常用的4个注解#xff08;作用相同#xff0c;推荐使用在不同分层上#xff09; ​ 依赖注入常用的注解 对象生命周期#xff08;作用范围#xff09;注解 3.纯注解模式 1.快速入门 导入依赖#xff1a; dependencies作用相同推荐使用在不同分层上 ​依赖注入常用的注解对象生命周期作用范围注解3.纯注解模式1.快速入门导入依赖dependencies dependency groupIdorg.springframework/groupId artifactIdspring-context/artifactId version5.0.2.RELEASE/version /dependency dependency groupIdcommons-logging/groupId artifactIdcommons-logging/artifactId version1.2/version /dependency dependency groupIdjunit/groupId artifactIdjunit/artifactId version4.10/version scopetest/scope /dependency /dependencies在配置文件中开启注解扫描?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beans xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xmlns:contexthttp://www.springframework.org/schema/context xsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd !-- 开启注解扫描 -- context:component-scan base-packagecom.qcby/ /beans创建接口和实现类package com.qcby.service; public interface UserService { public void hello(); } package com.qcby.service.Impl; import com.qcby.service.UserService; import org.springframework.stereotype.Component; /** * Component 作用是把当前类使用IOC容器进行管理如果没有指定名称默认使用当前类名userServiceImpl */ Component(value userService) public class UserServiceImpl implements UserService { Override public void hello() { System.out.println(hello IOC注解........); } }测试代码import com.qcby.service.UserService; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Demo { Test public void test1() { ApplicationContext context new ClassPathXmlApplicationContext(applicationContext.xml); UserService userService (UserService) context.getBean(userService); userService.hello(); } }运行结果我们在实现类上面加了Component注解它的作用是把当前类使用IOC容器进行管理value指定Bean的名称如果没有指定名称默认使用当前类名userServiceImpl2.常用注解bean管理类常用的4个注解作用相同推荐使用在不同分层上 ​Component普通的类Controller表现层Service业务层Repository持久层依赖注入常用的注解Value用于注入普通类型Stringintdouble等类型 ​Autowired默认按类型进行自动装配引用类型 ​Qualifier和Autowired一起使用强制使用名称注入 ​Resource JavaEE提供的注解也被支持。使用name属性按名称注入对象生命周期作用范围注解Scope生命周期注解取值singleton默认值单实例和prototype多例初始化方法和销毁方法注解PostConstruct相当于init-methodPreDestroy相当于destroy-method3.纯注解模式纯注解的方式是微服务架构开发的主要方式所以也是非常的重要。纯注解的目 的是替换掉所有的配置文件。但是需要编写配置类。配置类package com.qcby.config; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; /** * spring的配置类替换掉applicationContext.xml */ //声明这个类是配置类 Configuration //扫描指定的包结构 ComponentScan(valuecom.qcby) public class SpringConfig { }实体类package com.qcby.entity; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; Component public class Order { Value(北京) private String address; Override public String toString() { return Order [address address ]; } }测试方法import com.qcby.config.SpringConfig; import com.qcby.entity.Order; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Demo { /** * 需要加载配置类 */ Test public void test() { ApplicationContext context new AnnotationConfigApplicationContext(SpringConfig.class); //获取对象 Order order (Order) context.getBean(order); System.out.println(order); } }Configuration声明是配置类ComponentScan扫描具体包结构的Import注解Spring的配置文件可以分成多个配置的编写多个配置类。用于导入其他配置类Bean注解只能写在方法上表明使用此方法创建一个对象对象创建完成保 存到IOC容器中创建SpringConfig2在SpringConfig2当中配置德鲁伊连接池package com.qcby.config; import com.alibaba.druid.pool.DruidDataSource; import org.springframework.context.annotation.Bean; import javax.sql.DataSource; public class SpringConfig2 { Bean(name dataSource) public DataSource createDataSource() { DruidDataSource dataSource new DruidDataSource(); dataSource.setDriverClassName(com.mysql.jdbc.Driver); dataSource.setUrl(jdbc:mysql://localhost:3306/spring_db); dataSource.setUsername(root); dataSource.setPassword(root); return dataSource; } }在原来的配置类当中导入配置package com.qcby.config; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; /** * spring的配置类替换掉applicationContext.xml */ //声明这个类是配置类 Configuration //扫描指定的包结构 ComponentScan(valuecom.qcby) Import(value{SpringConfig2.class}) public class SpringConfig { }创建实体类package com.qcby.entity; public class Account { private int id; private String name; private Double money; public Account(String name, int id, Double money) { this.name name; this.id id; this.money money; } public Account() { } public int getId() { return id; } public void setId(int id) { this.id id; } public String getName() { return name; } public void setName(String name) { this.name name; } public Double getMoney() { return money; } public void setMoney(Double money) { this.money money; } Override public String toString() { return Account [id id , name name , money money ]; } }持久层:package com.qcby.dao; import com.qcby.entity.Account; import java.util.List; public interface AccountDao { ListAccount findAll(); } package com.qcby.dao.Impl; import com.qcby.dao.AccountDao; import com.qcby.entity.Account; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; import javax.sql.DataSource; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; Repository public class AccountDaoImpl implements AccountDao { Autowired private DataSource dataSource; Override public ListAccount findAll() { ListAccount list new ArrayList(); Connection conn null; PreparedStatement ps null; ResultSet rs null; try{ //获取连接 conndataSource.getConnection(); //编写sql String sqlselect * from account; //预编译 psconn.prepareStatement(sql); //查询 rs ps.executeQuery(); while(rs.next()){ Account accountnew Account(); account.setId(rs.getInt(id)); account.setName(rs.getString(name)); account.setMoney(rs.getDouble(money)); list.add(account); } } catch (Exception e) { throw new RuntimeException(e); }finally { try { if (rs ! null) rs.close(); if (ps ! null) ps.close(); if (conn ! null) conn.close(); } catch (Exception e) { e.printStackTrace(); } } return list; } }业务层package com.qcby.service.Impl; import com.qcby.dao.AccountDao; import com.qcby.entity.Account; import com.qcby.service.AccountService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; Service public class AccountServiceImpl implements AccountService { Autowired private AccountDao accountDao; Override public ListAccount findAll() { return accountDao.findAll(); } } package com.qcby.service; import com.qcby.entity.Account; import java.util.List; public interface AccountService { ListAccount findAll(); }测试代码Test public void test2() { ApplicationContext context new AnnotationConfigApplicationContext(SpringConfig.class); AccountService accountService context.getBean(AccountService.class); ListAccount listaccountService.findAll(); for(Account account:list){ System.out.println(account); } }结果可见我们在配置类2当中的配置也生效了
版权声明:本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!

wordpress 多网站网站建设启动大会

你是否曾想象过,仅凭一张照片就能穿越时光,看到自己未来或过去的模样?这不再是科幻电影的情节,而是InstantID带来的现实。这项创新技术让复杂的人脸年龄变化模拟变得触手可及,无需专业背景,零代码操作&…

张小明 2025/12/30 11:57:28 网站建设

霍尔果斯网站建设电子商务网站建设考卷

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容: 开发一个基于AI的夸克Cookie自动处理工具,功能包括:1. 自动识别夸克浏览器的Cookie验证机制;2. 智能解析Cookie数据并生成有效请求;3…

张小明 2025/12/30 22:16:14 网站建设

网站推广需要多少钱上海专业的网站

如何在 Kotaemon 中自定义检索器与重排序模块 在构建企业级智能问答系统时,一个常被低估的挑战是:如何让大语言模型(LLM)不“胡说八道”。尽管现代 LLM 能写出流畅的回答,但一旦涉及具体政策、技术参数或合规条款&…

张小明 2026/1/9 14:20:49 网站建设

win7如何做网站wordpress 记账

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容: 请生成一个面向新手的CentOS7 MySQL安装指南。要求:1.从系统更新开始逐步指导 2.包含每个命令的详细解释 3.提供安装过程中的截图示例 4.常见错误及解决方法 5.基础安全…

张小明 2025/12/30 22:16:07 网站建设

专门为98k做的网站长沙公司名称大全

第一章:揭秘Open-AutoGLM兼容性测试脚本的核心价值Open-AutoGLM作为面向大语言模型自动化推理优化的开源框架,其兼容性测试脚本在确保跨平台、跨硬件部署稳定性方面发挥着关键作用。该脚本不仅验证模型与不同后端(如ONNX Runtime、TensorRT&a…

张小明 2026/1/9 7:50:12 网站建设

深圳网站的优化网站维护哪些

智能图片去重工具:释放存储空间的完整解决方案 【免费下载链接】AntiDupl A program to search similar and defect pictures on the disk 项目地址: https://gitcode.com/gh_mirrors/an/AntiDupl 在数字信息爆炸的时代,图片文件占据了我们设备存…

张小明 2025/12/30 22:16:01 网站建设