Spring注解

【Spring】Spring 深入理解注解及spring对注解的增强 1.概述 深入理解注解及spring对注解的增强 2. 什么是注解? 代码中注释大家都熟悉吧,注释是给开发者看的,可以提升代码的可读性和可维护性,但是对于java编译器和虚拟机来说是没有意义的,编译之后的字节码文件中是没有注释信息的;而注解和注释有点类似,唯一的区别就是注释是给人看的,而注解是给编译器和虚拟机看的,编译器和虚拟机在运行的过程中可以获取注解信息,然后可以根据这些注解的信息做各种想做的事情。比如:大家对@Override应该比较熟悉,就是一个注解,加在方法上,标注当前方法重写了父类的方法,当编. 阅读详情

1. 在applicationContext.xml中配置

<context:component-scan base-package="ray.service,ray.serviceImpl"></context:component-scan>

1、@Component

@Component
是所有受Spring 管理组件的通用形式,@Component注解可以放在类的头上,@Component不推荐使用。

2、@Controller

@Controller对应表现层的Bean,也就是Action,例如:

1 @Controller
2 @Scope("prototype")
3 public class UserAction extends BaseAction<User>{
4 ……
5 }

使用@Controller注解标识UserAction之后,就表示要把UserAction交给Spring容器管理,在Spring容器中会存在一个名字为"userAction"的action,这个名字是根据UserAction类名来取的。注意:如果@Controller不指定其value【@Controller】,则默认的bean名字为这个类的类名首字母小写,如果指定value【@Controller(value="UserAction")】或者【@Controller("UserAction")】,则使用value作为bean的名字。

这里的UserAction还使用了@Scope注解,@Scope("prototype")表示将Action的范围声明为原型,可以利用容器的scope="prototype"来保证每一个请求有一个单独的Action来处理,避免struts中Action的线程安全问题。spring 默认scope 是单例模式(scope="singleton"),这样只会创建一个Action对象,每次访问都是同一Action对象,数据不安全,struts2 是要求每次次访问都对应不同的Action,scope="prototype" 可以保证当有请求的时候都创建一个Action对象

3、@ Service

@Service对应的是业务层Bean,例如:

1 @Service("userService")
2 public class UserServiceImpl implements UserService {
3 ………
4 }

@Service("userService")注解是告诉Spring,当Spring要创建UserServiceImpl的的实例时,bean的名字必须叫做"userService",这样当Action需要使用UserServiceImpl的的实例时,就可以由Spring创建好的"userService",然后注入给Action:在Action只需要声明一个名字叫“userService”的变量来接收由Spring注入的"userService"即可,具体代码如下:

1 // 注入userService
2 @Resource(name = "userService")
3 private UserService userService;

4、@ Repository

@Repository对应数据访问层Bean ,例如:

1 @Repository(value="userDao")
2 public class UserDaoImpl extends BaseDaoImpl<User> {
3 ………
4 }

@Repository(value="userDao")注解是告诉Spring,让Spring创建一个名字叫“userDao”的UserDaoImpl实例。

当Service需要使用Spring创建的名字叫“userDao”的UserDaoImpl实例时,就可以使用@Resource(name = "userDao")注解告诉Spring,Spring把创建好的userDao注入给Service即可。

1 // 注入userDao,从数据库中根据用户Id取出指定用户时需要用到
2 @Resource(name = "userDao")
3 private BaseDao<User> userDao;

三端口DC-DC变换器设计与光伏储能协同控制 三端口DC-DC变换器是面向分布式能源系统的关键功率变换拓扑,通过在光伏输入、储能电池与直流负载三个端口间实现动态能量分配,解决多源异步供电下的电压稳定与功率平衡问题。其核心原理基于双Boost半桥架构与双闭环协同控制,融合MPPT最大功率点跟踪与电池智能充放电管理,在保证30V高精度稳压(±0.5V)的同时,支持光伏主导供电与电池支撑供电的自动模式切换。该技术显著提升光储系统能量利用率与供电连续性,广泛应用于微电网、便携式交直流电源及离网型光储一体机等场景,是构建柔性直流配电与智能能源管理的基础硬件单元。 阅读详情

相关推荐

VTK实战笔记(1)在win11搭建VTK-9.4.2 + qt5.15.2 + VS2019_x64开发环境

Visual Studio 2019 + qt5.15.2 + vtk9.4.2

心向阳光,斗志昂扬! 978

【Spring MVC学习】详解spring mvc 3.0常用注解

Spring mvc的注解功能 1、@Controller——表示控制器 举例: @Controller public class SoftCreateController extendsSimpleBaseController {} 或者: @Controller("softCreateController") 说明:   @Controller负责注册一个bean到spring...

lmb55的专栏 1686

MATLAB 论文复现——面向紧急需求响应的规模化灵活资源负荷管理快速决策方法

其次,对灵活资源参与EDR的功率分配模型进行了建模,并提出了基于分层分布式共识算法的快速决策方法,对功率分配模型进行快速求解,以满足EDR实时性的需求,在保证较高精度的前提下提高了决策速度。[1]孔祥玉,杨至端,孙裕策,等.面向紧急需求响应的规模化灵活资源负荷管理快速决策方法[J/OL].电力系统自动化,1-13[2024-07-29].http://kns.cnki.net/kcms/detail/32.1180.TP.20240709.1843.002.html.关键词: 规模化灵活资源;

sjr1478的博客 374

SpringMVC之五:Controller

@Controller将一个bean标注为控制器,@RequestMapping标注一个控制器方法为url处理器。这大概是我们在日常开发工作中接触得最多的两个关键字了。本章介绍Controller类,尤其是@RequestMapping标注的处理器方法背后的工作原理。这部分介绍的知识,都是大家在日常工作中大概率能用上的知识,非常值得我们花点时间把它搞清楚。 本章的示例代码:SpringMVCSam...

longhuihu的专栏 1013

Win10+Anaconda环境下PaddleOCR保姆级安装指南(附常见报错解决方案)

本文提供Win10+Anaconda环境下PaddleOCR的保姆级安装指南,涵盖从环境配置到模型部署的全流程,特别针对常见报错提供解决方案。通过详细的步骤说明和实用技巧,帮助开发者快速完成PaddleOCR安装与测试,避免常见陷阱,提升OCR开发效率。

weixin_29096377的博客 377

机器人抓取(一)—— ROS MoveIt! 程序控制真实UR5机器人运动(python)

接上篇:ubuntu18.04 ros-melodic 安装 ur_robot_driver,驱动真实的 ur5 机器人 上文在 ubuntu 系统中配置了ur 机器人ros功能包 universal_robot 和驱动 Universal_Robots_ROS_Driver,并实现了用 moveit_rviz 拖动机器人运动。本文旨在学习如何使用 moveit python 用户接口moveit_commander,实现真实的ur5 机器人轨迹规划与控制。 1. ur5 机器人控制 参考:MoveIt »

zxxRobot的博客 1万+

JAVA面试常见问题总结(一)

一、Struts2 过滤器: StrutsPrepareAndExcuteFilter:可以在执行action前后添加自定义过滤器 工作原理: 用户请求HttpServletRequest 经过Filter过滤器 到ActionMapper——到ActionProxy——通过ConfigurationManager——查找struts.xml中具体的action类 通过A

mgatno1的专栏 413

RPA机器人技术原理初探

RPA初步

曲折旅程,艰难人生 2142

spring的一些常用注解

1、@Controller @Controller对应表现层的Bean,也就是Action@Controller public class Controller { }2、@ Service @Service对应的是业务层Bean@Service public class ServiceImpl implements Service { @Resource private Dao

我要修改昵称 429

【数据结构】考点二十八 : 二叉树的概念:二叉树的度、完全二叉树、满二叉数结点数、哈夫曼结点数、完全二叉树的深度

二叉树(Binary Tree)是一种常见的数据结构,它或者为空(null),或者由一个根节点(root node)和两个子树(left subtree 和 right subtree)组成,这两个子树又分别是一棵二叉树。二叉树的每个节点最多有两个子节点,通常称为左子节点(left child)和右子节点(right child)。

超越的博客 1738

ENVI_基于无人机数据的果树提取与相对健康监测

一、数据处理 1.计算植被指数VDVI或GDVI:Band Math / Spectral Indices 2.制作掩膜:ROI——Threshold——Subset 二、单地块果树提取 1.绘制感兴趣区shp 2.确定果树提取参数:Count Crops 3.输出果树矢量文件:Convert Crops To Shapefile 4.果树相对健康程度分析:Calculate Crop Me...

提供技术服务、资源分享 429690771 1400

Spring之注解开发

Spring是轻代码而重配置的框架,配置比较繁重,影响开发效率,所以注解开发是一种趋势,注解代替xml配置文件可以简化配置,提高开发效率。

write less , do more 6322

低功耗设计-Power Switch

power gating与power switch cell

m0_61544122的博客 1万+

Spring 注解

Spring注解的使用

wo_1ca的博客 2327

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

Spring——基于注解的配置

Spring注解配置 从 Spring 2.5 开始就可以使用注解来配置依赖注入。使用注解的方式使我们无需在XML中配置一个Bean引用,更加简单和方便。 注解配置默认情况下在Spring中是关闭的,我们需要在配置文件中使用&lt;context:annotation-config/&gt;激活它。 如下spring-config.xml文件: &lt;?xml version="1....

Gavin 2万+

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

Spring的注解 @Bean用法

随着SpringBoot的流行,基于注解式开发的热潮逐渐覆盖了基于XML纯配置的开发,而作为Spring中最核心的bean当然也能够使用注解的方式进行表示。所以本篇就来详细的讨论一下作为Spring中的Bean到底都有哪些用法。 @Bean 基础声明 Spring的@Bean注解用于告诉方法,产生一个Bean对象,然后这个Bean对象交给Spring管理。产生这个Bean对象的方法Spring只会调用一次,随后这个Spring将会将这个Bean对象放在自己的IOC容器中。 S

应无所住而生其心 2万+

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

Spring 中的常用注解

Spring 作为 Java 企业级开发中最广泛使用的框架之一,以其强大的功能和灵活性为开发者提供了高效的开发体验。在 Spring 中,注解(Annotation)是其核心机制之一,它简化了配置文件的繁琐操作,通过声明的方式实现了依赖注入等功能。本篇文章将介绍 Spring 框架中一些常用的注解,并结合实际场景详细说明它们的使用方法与作用。这不仅可以帮助初学者快速掌握 Spring 的基本用法,也能为有经验的开发者提供一个参考,帮助在实际开发中更高效地使用这些工具。

栗筝i的博客 2511

Spring 注解解析

@Transactional 通过 AOP 动态代理和 事务管理器实现声明式事务管理,开发者需关注传播行为、隔离级别、异常处理等核心属性,避免同类调用、非 public 方法等失效场景。在复杂场景下,结合编程式事务可提升灵活性和可控性。

肢解生活,直达本质;凡所有相,皆是虚妄。 1782

Spring 中所有注解

Spring中的注解主要分为两类: 类级别的注解:如@Component、@Repository、@Controller、@Service以及JavaEE6的@ManagedBean和@Named注解,都是添加在类上面的类级别注解。 类内部的注解:如@Autowire、@Value、@Resource以及EJB和WebService相关的注解等,都是添加在类内部的字段或者方法上的类内部注解。 ...

weixin_44371237的博客 2541

Spring注解开发

Spring IoC注解式开发通过使用注解来简化XML配置,Spring6倡导全注解开发。注解的定义和使用包括定义注解及其属性、使用注解以及通过反射机制读取注解。常见的声明Bean的注解有@Component、@Controller、@Service和@Repository。通过反射机制,可以扫描指定包下的类,判断类上是否有特定注解,并实例化相应的对象。这种方式使得开发更加简洁和高效,减少了繁琐的XML配置。

weixin_63855746的博客 880

【Spring】Spring注解大全

1. 注解是什么? 其实 Java 的注解还是特别常见的,像平时用到的 @Override、@Deprecated 等都是注解的一种表现方式。 对于注解官方解释:为我们在代码中添加信息提供了一种形式化的方法,使我们可以在稍后的某个时刻非常方便地使用这些数据。 2. 注解作用 可以书写更加干净易读的代码,简化重复的工作,比如 Spring 的注解; 另外还提供编译期类型检查,比如外汇...

小七的博客 920

Spring常用注解全集

第一个是,@Target,表明注解所修饰的对象范围,即,该注解可以用在哪里。第二个是,@Retention,表示注解被保留的时间长短,也就是注解的生命周期。

m0_73413202的博客 1344

注解及Spring系列注解

1.定义:使用@interface2.定义属性:类名+名字()3.赋默认值:default + 默认值4.修饰:使用需要的元注解注解为代码提供元数据,用于标记、约束、文档化和框架集成,可被编译器或运行时处理。熟练运用注解,可以提高开发的效率。

m0_75014903的博客 1130

Spring基于注解开发

非自定义Bean不能像自定义Bean一样使用@Component进行管理,非自定义Bean要通过工厂的方式进行实例化使用@Bean标注方法即可,@Bean的属性为beanName,如不指定为当前工厂方法名称。这里要注意的点是配置了@Bean的方法的类是要被Spring管理的。还有就是要是我们想在方法中注入属性该如何操作呢?在这里我们可以使用@Value进行普通属性注入,还可以使用@Qualifier进行指定名称的对象注入,但是@Qualifier不是需要配合@Autowired一起使用吗?

m0_65078452的博客 1495
上一篇: jsp中的basePath和path (绝对路径 相对路径)
下一篇: Struts中jsp和action中参数传递总结
Uno2
博客等级 码龄15年 8粉丝 128原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值