bean创建的四种方式 springAOP的两种配置方式 xml版本自动注入bean 全注解自动注入对象 bean对象属性注入的三种方式 spring依赖注入

spring注入Bean的几种方式 阅读详情

1.Spring 的依赖注入

1.1 什么是spring

它是一个轻量级和IOC(DI)和AOP的 容器框架

IOC:控制反转 以前是我们自己new对象 现在创建bean的方式,是交给spring帮我们创建

DI:依赖注入 spring它是容器框架,是一个bean(装对象的)容器框架,spring帮我创建好对象之后,在把我们创建好的对象 注入到相应的类里面 这个过程

AOP: 面向切面的编程

​ AOP使用场景:

​ 事务管理

​ 日志管理

​ 性能控制

​

1.2 spring的DI依赖注入

1.2.1 set方式注入(最开始)

以前

applicationContext.xml配置 通过全限定名找到这个类 给这个类创建好对象通过set方法传进去
< bean id=“userService” class="…UserServiceImpl">

    
  
UserController{

​	private UserService userService;
// 默认就是 通过set属性传值 
​	void setUserService(UserService userService){

​    		this.userServicer = userService;
     }
}

1.2.2构造器注入

​ index索引注入

 注意value 值对应类型   索引从0开始
<constructor-arg index="0" value="123"></constructor-arg>

name 参数名字注入

  
<constructor-arg name="id" value="456"></constructor-arg>

type类型注入

若是有多个相同的类型 依次添加值即可
 <constructor-arg type="java.lang.Long" value="111"></constructor-arg>

1.2.3 类里面属性注入

属性注入:  最常用就是基本类型和引用类型注入
 //基本数据类型注入
   <property name="id" value="111"></property>
   <property name="name" value="康康"></property>
// ref  引用一个对象    
   <property name="hellobean" ref="hb"></property>
   <property name="hellobean">
     //直接注入一个对象
          <bean class="xxxx"></bean>
    </property>

 <!-- 数组注入方式1  属性注入底层还是使用的set方法-->
<property name="strings" value="A,B,B,B"></property

                <!-- 数组注入方法二-->
<property name="strings">
<array >
<value>是尼玛 </value>
<value>是我</value>
</array>
</property>
 <!-- 集合注入方法-->
<property name="list">
<list>
    <value>你让我说点啥好</value>
    <value>你让我说点啥好</value>
    <!--创建的对象 每次都是不同的对象-->
    <bean class="_01_di.cn.itsource.HelloBean"></bean>
    <bean class="_01_di.cn.itsource.HelloBean"></bean>
    <!-- 引用的两个地址是一样的 -->
    <ref bean="hello"></ref>
    <ref bean="hello"></ref>
</list>

 <!-- 集合注入方法 set-->
<property name="set">
 <set>
 <value>小胡</value>
 <value>小胡</value>
 // 注入一个bean对象 注入的对象地址都是不同的
      <bean class="_01_di.cn.itsource.HelloBean"></bean>
     <bean class="_01_di.cn.itsource.HelloBean"></bean>
     // 引用的地址是一样的
     <ref bean="hello"></ref>
     <ref bean="hello"></ref>
 </set>


<!-- map传值-->
<property name="hp">
<map>
<entry key="小胡" value="小幸福"></entry>
<entry key="小长" value="陆小凤"></entry>
</map>

</property>


注入properties
// 不支持中文
<property name="prop1">
<value>
driverClassName=com.mysql.jdbc.Driver
username=qiang强
</value>
</property> 
<property name="prop2">
//支持中文
<props>
<prop key="driverClassName">com.mysql.jdbc.Driver</prop>
<prop key="username">qiang强</prop>
</props>
</property>

1.3 XML版本自动注入

可以byName – 按照名称自动注入
或者byType注入 – 按照类型自动注入(默认就是bytype)
根节点beans default-autowire=“byName” 对当前配置文件的所有bean都生效
子节点bean autowire="byType"只对当前bean生效

通常如果我们需要给一个bean注入另一个bean时都是通过set方法或者构造方法进行注入,其实Spring也支持bean的自动注入。在定义bean时我们可以通过bean元素的autowire属性来指定是否需要给当前bean来自动注入其所关联的bean。autowire属性的可选值有四个。

  • no:默认值。表示不进行自动注入。
    • byName:根据名称进行自动注入。如beanA有一个setBeanB()方法,指定autowire=”byname”时Spring将自动在bean容器中寻找名为“beanB”的bean通过setBeanB方法自动注入给beanA。
    • byType:根据类型进行自动注入。如beanA有一个setBeanB(BeanB b)方法,指定autowire=”byType”时Spring将自动在bean容器中类型为BeanB的bean通过setBeanB()方法注入给beanA。但是如果此时容器中有两个以上类型为BeanB的bean时就将抛出异常。解决方法若是有两个相同类型的baen的时候可以使用 name来注入
    • constructor:等同于byType,只是当指定autowire=”constructor”时表示将通过构造方法根据类型进行自动注入。

xml详解


    <bean id="userController" class="_02_autoxml.cn.itsource.UserController" autowire="byType">
        <!--<property name="userService" ref="service"></property>-->
    </bean>

    <bean id="userDao" class="_02_autoxml.cn.itsource.UserDao"></bean>

    <bean id="userService" class="_02_autoxml.cn.itsource.UserServiceImpl">
       <!-- <property name="userDao" ref="dao"></property>-->
    </bean>


1.4 XML版本注解注入

扫描包路径  扫描注解:
        @Component 普通类上面打的注解(普通仓库), @Repository, @Service,@Controller
<context:component-scan base-package="_03_annoxml.cn.itsource"></context:component-scan>

如果一个接口有两个实现情况下:

       方案一:一个接口有多个实现的情况下面 通过名字去区分
       方案二:通过Resource这个注解
       区别:@Autowired和@Qualifier 都是属于spring的注解 ,
          Resource使用jdk的注解 推荐使用Autowired和Qualifier,可以和spring进行无缝衔接
          autowired默认是根据类型匹配,如果类型匹配不上在根据名字匹配
          而Resource默认根据名字匹配,名字匹配不上就匹配类型

2 Spring里面AOP功能

2.1 什么是springAOP

aop: 面向切面的编程
而AOP的出来并不是要取代OOP(面向对象编程),它只是作为一个补充的角色过来,来解决一些咱们OOP(面向对象)解决不了(或者说很难解决)的一些代码。
AOP的使用只存在于一些特定的场合(具有横切逻辑的应用场合),横切逻辑这个解释可能比较抽象,咱们说得再具体一点,AOP可以用于事务管理,日志管理,性能监测等地方。

2.2aop的实现

aop底层使用代理模式实现

jdk代理模式 --有接口情况下

cglib动态代理 --没有接口情况下

2.3 springaop配置方式

xml版本的配置

// 扫描主键
<context:component-scan base-package="_04_aopxml"></context:component-scan>
    <aop:config>
       // 在哪里切入
        <aop:pointcut id="pointcut" expression="execution(* _04_aopxml.I*Service.*(..))"></aop:pointcut>
        <!--配置通知  引用事务管理--> 
        <aop:aspect ref="txManager">
            <!-- 前置通知-->
            <!--<aop:before method="begin" pointcut-ref="pointcut"></aop:before>
            &lt;!&ndash; 后置通知&ndash;&gt;
            <aop:after-returning method="commit" pointcut-ref="pointcut"></aop:after-returning>
            &lt;!&ndash; 最终通知&ndash;&gt;
            <aop:after method="close" pointcut-ref="pointcut"></aop:after>
            &lt;!&ndash; 异常通知&ndash;&gt;
            <aop:after-throwing method="rollback" throwing="e" pointcut-ref="pointcut"></aop:after-throwing>-->
            <!-- 环绕通知-->
            <aop:around method="around" pointcut-ref="pointcut"></aop:around>
        </aop:aspect>
    </aop:config>
    <bean id="txManager" class="_04_aopxml.TxManager"></bean>

注解版本配置

@Component
@Aspect
public class TxManager {

   // 在哪里切入 执行
    @Pointcut("execution(* _05_aopanno.I*Service.*(..))")
    //切入点
    public void pointcut(){}

    //<aop:before method="begin" pointcut-ref="pointcut"></aop:before>
    //@Before("pointcut()")
    public void begin(){
        System.out.println("开启事务");
    }

  ///  @After("pointcut()")
    public void commit(){
        System.out.println("提交事务");
    }
    //@AfterReturning("pointcut()")
    public void close(){
        System.out.println("关闭");
    }
    //所有的异常的父类
   // @AfterThrowing(value = "pointcut()",throwing = "e")
    public void rollback(Throwable e){
        System.out.println("回滚事务"+e.getMessage());
    }

    //环绕通知
    @Around("pointcut()")
    public void around(ProceedingJoinPoint joinPoint){
        try {
            begin();
            //调用方法
            joinPoint.proceed();
            commit();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }finally{
            close();
        }
    }
}

3 Bean的创建方式

以前模式:通过new 对象方式创建

spring方式 :spring帮我们创建bean 。–创建bean的方式

3.1 Bean创建方式一

通过无参数的构造方法来创建bean --最常用方式

配置

<bean id="mybean" class="_06_bean.Mybean"></bean>

3.2 Bean创建方式二

FactoryBean 工厂对象

 //实现 FactoryBean
public class MybeanFactoryBean implements FactoryBean<Mybean> {
    //返回对象
    public Mybean getObject() throws Exception {
        return new Mybean("xxx", "18");
    }
   
    public Class<?> getObjectType() {
        return Mybean.class;
    }
    //是否单例
    public boolean isSingleton() {
        return true;
    }
}

3.3 Bean创建方式三

通过类里面定义静态方法 来获取bean

public class MybeanFactory {

    public static Mybean getBean(){
        return new Mybean();
    }
}
<bean class="_06_bean.MybeanFactory" factory-method="getBean"></bean>

3.4 Bean创建方式四

public class MybeanFactory1 {
    //普通方式
    public  Mybean getBean1(){
        return new Mybean();
    }
}
<bean class="_06_bean.MybeanFactory1" id="mybeanFactory1"></bean>
    <bean factory-bean="mybeanFactory1" factory-method="getBean1"></bean>
public class MybeanFactory1 {
    //普通方式
    public  Mybean getBean1(){
        return new Mybean();
    }
}
<bean class="_06_bean.MybeanFactory1" id="mybeanFactory1"></bean>
    <bean factory-bean="mybeanFactory1" factory-method="getBean1"></bean>
深入STM32F429 DCMI接口:从OV7725时序图到HAL库DMA传输的底层逻辑解析 本文深入解析STM32F429 DCMI接口与OV7725图像采集的硬件协同机制,从时序匹配到HAL库DMA传输的底层逻辑。详细探讨了DCMI控制器如何通过精确的时序捕捉和智能数据重组完成高效图像采集,并提供了性能优化的实战技巧,包括双缓冲机制和时钟域协同优化,助力开发者提升嵌入式视觉系统性能。 阅读详情

相关推荐

Spring-注入依赖,AOP,自动注入Bean

Spring-注入依赖,AOP,自动注入Bean。入门。为什么没有0积分?

bean属性注入方式及AOP的概述

bean属性注入方式及AOP的概述

skqpp1314的博客 474

CentOS 7 常用命令大全

CentOS中Linux命令

m0_63075763的博客 2021

bean属性注入的方式及AOP的概述

AOP Aspect Oriented Programing 面向切面编程 ----是一种编程思想:在不修改源代码的情况下,对目标方法进行增强使用动态代理的机制,在不修改源代码的情况下,对某些方法进行动态的增强传统开发:发现很多重复的代码:解决方案:众多方法中的所有共有代码全部抽取出来,放置到某个地方集中管理,然后在具体运行时,再由容器动态织入这些共有的代码AOP解决之后:好处:1:程序员在编写具体的业务逻辑处理方法时,只需关心核心的业务逻辑处理,既提高了工作效率,又使代码变更简洁优雅。

m0_73030012的博客 556

英飞凌TC397芯片时钟树详解:从EB-tresos配置到PLL锁相环实战避坑

本文详细解析了英飞凌TC397芯片的时钟树架构,从EB-tresos配置到PLL锁相环的实战应用,帮助工程师避免常见配置陷阱。文章深入探讨了时钟源层、频率转换层和分配网络层的设计原理,提供了PLL配置的数学公式和EB-tresos关键容器的配置要点,并分享了调试技巧与性能优化实践,适用于MCU开发中的时钟管理需求。

weixin_42524226的博客 214

(完整版)从零开始教你玩转ComfyUI-保姆级部署教程-手把手带你ComfyUI工作流搭建

ComfyUI就像拥有一支神奇魔杖,可以轻松创造出令人惊叹的AI生成艺术。从本质上讲,ComfyUI是构建在Stable Diffusion之上的基于节点的图形用户界面(GUI),而Stable Diffusion是一种最先进的深度学习模型,可以根据文本描述生成图像。但ComfyUI真正特别之处在于,它如何让像你这样的艺术家释放创造力,将你最疯狂的想法变为现实。想象一下有一块数字画布,你可以通过连接不同的节点来构建自己独特的图像生成工作流,每个节点代表一个特定的功能或操作。

php源码 4万+

aop代理方式引起的spring注入bean(实现类)与获取bean(实现类)出错

描述:   现象一 :A 为 接口,AImpl 为 A 的实现类,且 AImpl 受 aop 扫描,且 aop 无特殊配置      此时若:Spring 中 注入 AImpl 类型的bean,获取一样 报错          Spring 中 注入 A 类型的bean ,获取一样 成功   现象二 :B 没有实现接口,B受 aop 扫描,且 aop无特殊配置...

weixin_30323961的博客 434

Windows10内核逆向-2-KiSystemCall64的调用流程剖析

本文深入剖析了Windows 10 x64系统中KiSystemCall64函数的完整调用流程。从用户态触发syscall指令开始,详细讲解了如何通过swapgs切换内核栈、构建KTRAP_FRAME陷阱帧、进行SMAP/CFG等安全检查,到最终通过SSDT寻址并执行目标系统服务的全过程,揭示了系统调用从Ring 3到Ring 0切换的核心机制与安全设计。

weixin_29209805的博客 89

Spring知识总结(期末复习版)

Spring基础知识,适合大学生期末复习

m0_73213501的博客 865

iOS面试题大全(附带答案)

C语言相关面试题 1.static有什么用途? 答案:在C语言中,static主要定义全局静态变量,定义局部静态变量,定义静态函数。 static 属于静态变量,使用它修饰的变量生命周期是整个源程序。 @1.在函数体内的 static 变量的作用范围为该函数体,该变量的内存只被分配一次,因此其值在下次调用时仍维持上次的值; @2.在模块内的 static 全局变量可以被模块内所有函数访问,但不能被模块外其它函数访问; @3.在模块内的 static 函数只被这一模块内的其它函数调用,这个函数的使用范围被限

IOSNF的博客 2434

IgH EtherCAT 从入门到精通:第 3 章 第一次运行 Hello EtherCAT

摘要:启动EtherCAT主站实践指南 本章详细介绍了如何配置和启动EtherCAT主站系统,主要内容包括: 配置文件详解:讲解/etc/ethercat.conf中的关键配置项,包括主网卡指定、冗余备份设置和网卡驱动选择。 启动流程:说明通过systemd服务和手动命令两种方式加载内核模块,重点介绍了ec_master和ec_generic模块的加载顺序及验证方法。 命令行工具:列举ethercat工具的常用命令,如查看主站状态、扫描从站设备、读取PDO/SDO数据等。 示例程序:简要介绍内核态示例min

从事嵌入式linux android驱动开发,拥有全栈能力 327

笔记05 SpringAOP

1.Spring 的依赖注入 1.1 什么是spring 它是一个轻量级和IOC(DI)和AOP的 容器框架 IOC:控制反转 现在创建bean的方式,是交给spring帮我们创建。(理解) DI:依赖注入 spring它是容器框架,是一个bean(装对象的)容器框架,spring帮我创建好对象之后,在把我们创建好的对象 注入到相应的类里面 这个过程(理解) 1.2 spring的DI依赖注入 ...

weixin_45654934的博客 125

番外篇 | 关于YOLO11算法的改进点总结

关于YOLO11算法的改进点总结。🌈

小哥谈 2322

深入浅出spring boot 学习笔记

目录全注解下的Spring IOCIOC简介装配Bean依赖注入(Dependency Injection ,DI)注解@Autowired消除歧义性——@Primary和@Quelifier带有参数的构造方法类的装配生命周期使用属性文件application.properties@ConfigurationProperties使用载入属性文件条件装配BeanBean的作用域使用@Profile引...

zj20165149的博客 209

LeetCode 面试经典 150 题回顾

最经典 150 题,掌握面试所有知识点 面试经典 150 题 : 题目分析、解题思路、复杂度分析、代码展示

曾经“等你生日那天”都遥远得像未来,如今却可欢愉的挥手说“下个十年见” 8万+

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

Spring5学习笔记

1.Spring5 框架概述 Spring 是轻量级的开源的 JavaEE 框架 Spring 可以解决企业应用开发的复杂性 Spring 有两个核心部分:IOC 和 AOP (1)IOC:控制反转,把创建对象过程交给 Spring 进行管理 (2)AOP:面向切面,不修改源代码进行功能增强 Spring 特点 (1)方便解耦,简化开发 (2)AOP编程支持 (3)方便程序测试 (4)方便集成各种优秀框架 (5)方便进行事务操作 (6)降低Java EE API的使用难度 2.IOC 什么是IOC?

惟一||.唯一 的博客 521

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

Spring5框架

Spring5 一、IOC容器 1、什么是IOC(控制反转) a)把对象创建和对象之间的调用过程,交给Spring进行管理 b)使用IOC目的:为了降低耦合度 2、IOC底层 a)xml解析、工厂模式、反射(通过xml获取到类的全限名,再用反射通过class.forName(“全限名”))获取到class对象。 3、Spring提供的IOC容器实现的两种方式(两个接口) a)BeanFactory接口:IOC容器基本实现是Spring内部接口的使用接口,不提供给开发人员进行使用(加载配置文件时候不会创建对象

lovewangyihui的博客 147

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

深度学习工具包 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")

IDEASpring1:概念、类上的注解、实现类的注解、配置类替代xml、案例、引入外部文件

spring核心功能 IOC:控制反转,原来擦黄健对象用new,现在把创建对象的过程交给spring AOP:面向切面编程。在不修改源码的情况下更改功能。 特点 1.方便解耦,简化开发 2.aop支持 3.方便测试 4.方便和其它框架整合 IOC (1)控制反转,把对象的创建和调动的过程,交给spring (2)目的:降低耦合度、 (3)BeanFactory (4)底层原理:XML解析、工厂模式 Spring实现IOC的方法: 1.BeanFactory,内部接口,无法外部使用 2.App..

qq_31897241的博客 741

AOP

Spring AOP-- 构造注入&代理模式&AOP 依赖注入;(掌握) ​ 什么是依赖注入?(DI),是Spring里面的特性 ​ 依赖注入就是一个类里面依赖于另外一个类,然后Spring把类的对象创建出来,然后再注入到相应的类里面,这个过程就叫做依赖注入。 ​ 依赖注入的方式: ​ 1. Set方法注入 ​ 2. 构造器注入 ​ 3. 注解方式注入 要使用依赖注入就要...

Kira_7的博客 269

Spring AOP中bean的定义与装配

Spring AOP中bean的定义与装配学习Spring AOP 时,做了个小demo,结果却出了点小问题:去了 segmentfault 问了一下: https://segmentfault.com/q/1010000011886470问题刚发布完,我突然就意识到了什么,于是乎,改了代码如下:实现类 PerformanceImpl 改为 /** * @author itguang * @cr

只有变秃 才能变强 1517

Spring AOP切面类依赖注入的正确姿势

在某些场景下需要将Spring的Bean注入到Spring AOP切面的属性中。 类似下面代码: @Aspect public class SomeAspect { @Inject private SomeService someService; @Before("some_pointcut_here") public void doInterception() { ...

悟鸣的技术博客 7232

[转载]aop代理方式引起的spring注入bean(实现类)与获取bean(实现类)出错

作者:枫红一叶出处:http://www.cnblogs.com/peak-c/本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。描述:  现象一 :A 为 接口,AImpl 为 A 的实现类,且 AImpl 受 aop 扫描,且 aop 无特殊配置       此时若:Spring 中 注入 AImpl 类型的be...

qq1025551832的博客 1201

Spring自定义实现Aop或使用@Transaction,Bean对象注入为null的情况分析

一、前言 有时我们需要通过使用Aop的方式对我们的程序进行切面监控,比如Service执行时间,记录log等。常用的两种方式一个是写execution表达式,还一种是自定义注解。但是在使用的过程中,有时我们会发现原本没问题的Service,现在使用里面注入的bean对象变为null了,针对这种情况我们分析一下。 二、情景再现 自定义注解Monitor public @interface Monitor { } 自定义Advice @Aspect @Component @EnableAsp.

yytree123的博客 2249

Spring中Bean注入与获取几种方式详解

【1】单独bean,属性都为简单类型。通过属性注入 &amp;lt;!-- 通过属性注入,利用反射,实例化bean。类必须有无参构造方法 --&amp;gt; &amp;lt;bean id=&quot;hello&quot; class=&quot;com.web.hello.HelloWorld&quot;&amp;gt; &amp;lt;property name=&quot;name&am

小小默:进无止境 6434
上一篇: Jpql语句 乐观锁 悲观锁 数据库的三范式 事务并发 jpa经验
下一篇: Spring SpringMVC JPA整合配置
羡慕班长
博客等级 码龄7年 5粉丝 27原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值