免费快速建站网站用土豆做美食的视频网站

张小明 2026/1/9 19:03:12
免费快速建站网站,用土豆做美食的视频网站,建设个网站需要多少钱,安徽省交通运输厅目录 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进行投诉反馈,一经查实,立即删除!

鞍山市信息网站重庆手机网站推广流程

今天记录一个比较隐晦不好排查的问题,写了个查询功能, 结果用户反馈在页面点查询得5 -6分钟才能出来,执行的过程中把数据库执行的sql取出来,字段替换掉在数据库直接执行很快不到1秒,结果在解决的过程中1.尝试的给sql的…

张小明 2025/12/31 10:55:31 网站建设

佛山大型网站设计公司626969com域名信息查询

C# WPF界面设计:为ACE-Step打造桌面级音乐创作软件 在AI技术不断渗透创意产业的今天,普通人也能“一键作曲”已不再是科幻场景。像ACE-Step这样的开源AI音乐生成模型,正让文本描述瞬间转化为旋律成为现实——只需输入“一段轻快的钢琴曲&…

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

网站建设视频教程最新做瞹瞹瞹免费网站

一、量化投资概述 1.1 什么是量化投资? 定义: 通过数学模型和计算机程序,根据数据发出买卖决策信号的投资方法。 特点: 基于数据 系统化 程序化 客观理性 1.2 算法交易与程序化交易 算法交易: 使用算法执行交易 优化交易成本 减少市场冲击 程序化交易: 用程序自动执行…

张小明 2025/12/31 17:27:16 网站建设

微信html5模板网站唐山广告设计制作公司

OpenColorIO-Configs 完整颜色管理配置指南 【免费下载链接】OpenColorIO-Configs Color Configurations for OpenColorIO 项目地址: https://gitcode.com/gh_mirrors/ope/OpenColorIO-Configs 在当今数字影像制作领域,颜色管理是确保视觉效果一致性的关键环…

张小明 2025/12/31 16:38:25 网站建设

怎样不用代码就能建网站模板建站价格

金刚山属太白山脉核心段,横跨金刚郡、通川郡等多地及韩国麟蹄郡,总面积达530平方公里。这座秀丽名山东西绵延40公里,南北纵贯60公里,海拔千米以上山峰逾60座,主峰毗卢峰以1638米的海拔雄踞群峰之上。山名源自佛教“金刚…

张小明 2026/1/2 4:57:19 网站建设

什么推广网站好律所网站建设

Minecraft数据编辑大师课:NBTExplorer让存档修改变得如此简单 【免费下载链接】NBTExplorer A graphical NBT editor for all Minecraft NBT data sources 项目地址: https://gitcode.com/gh_mirrors/nb/NBTExplorer 想要完全掌控你的Minecraft世界吗&#x…

张小明 2026/1/5 0:15:10 网站建设