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

如何优化网站推广张家港市做网站的公司

如何优化网站推广,张家港市做网站的公司,wordpress背景飘带,建个企业网站一年多少钱目录 前言 Component:通用的注解! Bean 引入第三方的类 Configuration 前言 首先:我们先简单描述一下这三个的作用 Component注解表明一个类会作为组件类,并告知Spring要为这个类创建bean。 Bean注解告诉Spring这个方法将会…

目录

前言

@Component:通用的注解!

@Bean

引入第三方的类

@Configuration


前言

首先:我们先简单描述一下这三个的作用

@Component注解表明一个类会作为组件类,并告知Spring要为这个类创建bean。

@Bean注解告诉Spring这个方法将会返回一个对象,这个对象要注册为Spring应用上下文中的bean。通常方法体中包含了最终产生bean实例的逻辑。用于显式声明单个bean,而不是让Spring像上面那样自动执行它。它将bean的声明与类定义分离,并允许您精确地创建和配置bean。另外@Bean注解的方法返回值是对象,可以在方法中为对象设置属性。

@Configuration即用来代替Spring配置文件的,它就是一个@Component组件,接收一个value值也就是bean的名字,value可以不填。

但是上面的讲解都只是冰冷的概念,决定重新捡起来,好好研究这些配置,因为虽说平时自己写的时候都是机械性的,甚至是试探性的,但是还是要更深入一点。

为什么要先讲这三个概念,是因为他们之间的关系,有些你中有我我中有你,最好先有一个大概的了解。

@Component:通用的注解!

通俗的讲:@Compent就是说,是标注这个类是一个组件类,只要你想将你写的类交给Spring容器来管理,那就可以用它!

1、@controller: controller控制器层(注入服务)

2、@service : service服务层(注入dao)

3、@repository : dao持久层(实现dao访问)

为什么摆出上面这三个,因为他们其实就是@Compent,例如我们进入到@Controller里看一下,发现在Controller上就有一个@Component,剩下的两个同理

package org.springframework.stereotype;
​
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor;
​
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Controller {@AliasFor(annotation = Component.class)String value() default "";
}

写一个简单的功能,先写一个类,用@Compent注解上

package com.newcrud.learn;
​
import lombok.Data;
import org.springframework.stereotype.Component;
​
@Data
@Component
public class KangShiFuTwo {String name;Integer age;public void getKangShiFuTwo(){System.out.println("getKangShiFuTwo");}
}

写一个测试类,可以用@Autowired获取到没说明已经在Spring容器里了

package com.newcrud.learn;
​
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.annotations.Test;
​
@SpringBootTest
​
public class KangShiFuTwoTest extends AbstractTestNGSpringContextTests {@AutowiredKangShiFuTwo kangShiFuTwo;@Testpublic void testGetKangShiFuTwo() {kangShiFuTwo.getKangShiFuTwo();}
​
}

看执行结果

getKangShiFuTwo

@Bean

我们先看一下@Bean的源码

package org.springframework.context.annotation;
​
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.core.annotation.AliasFor;
​
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Bean {@AliasFor("name")String[] value() default {};
​@AliasFor("value")String[] name() default {};
​/** @deprecated */@DeprecatedAutowire autowire() default Autowire.NO;
​boolean autowireCandidate() default true;
​String initMethod() default "";
​String destroyMethod() default "(inferred)";
}

emmm,他没有@Compent

引入第三方的类

那,我们首先创建一个没有@Compent的类

package com.newcrud.learn;
​
import lombok.Data;
​
@Data
public class KangShiFuOne {String name;Integer age;public void getKangShiFuOne(){System.out.println("getKangShiFuOne");}
}

 再写一个配置类


​import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
​
@Configuration
public class MyConfiguration {@Beanpublic KangShiFuOne getKangShiFuOneConfig(){return new KangShiFuOne();}
}

再来写一个测试类

package com.newcrud.learn;
​
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.annotations.Test;
@SpringBootTest
public class MyConfigurationTest extends AbstractTestNGSpringContextTests {@AutowiredKangShiFuOne kangShiFuOne;
​@Testpublic void testGetKangShiFuTwoConfig() {kangShiFuOne.getKangShiFuOne();}
}

结果如下

getKangShiFuOne

能被@Autowired获取到,是因为@Configuration的作用,那@Bean的作用是什么呢,因为他有一个@Compent没有的功能,那就是他可以将不在jar包里的类放到Spring容器里,比如说你在pom文件中引入的工具包,你并不能把@Compent放到里面去对不对,那你就可以通过@Bean的方式来获取。

这个涉及到 spring boot 的自动装载的概念,当你的 bean 不在你 jar 包的扫描目录下时,是没法实例化的给 spring 管理的。

@Configuration

官方文档描述:用@Configuration注释类表明其主要目的是作为bean定义的源

我们再来看一下@Configuration的源代码,发现在上面也有一个@Compent,那就说明,它也有@Compent的功能

package org.springframework.context.annotation;import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor;
import org.springframework.stereotype.Component;@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration {@AliasFor(annotation = Component.class)String value() default "";boolean proxyBeanMethods() default true;
}

通俗的来讲,这个就是代表它所注释的类是一个配置类,但什么才是配置类呢,上面我们也看到了@Configuration好像也就是个@Compent啊,没啥大用

那我们先来编写两个类

package com.newcrud.learn;import lombok.Data;@Data
public class KangShiFuFour {public  KangShiFuFour(){System.out.println("getKangShiFuFour");}String name;Integer age;
}
package com.newcrud.learn;import lombok.Data;@Data
public class KangShiFuThree {public  KangShiFuThree(){System.out.println("getKangShiFuThree");}String name;Integer age;
}

再来写我们的@Configuration

package com.newcrud.learn;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class MyConfigurationTwo {@Beanpublic KangShiFuThree getKangShiFuThree(){return new KangShiFuThree();}@Beanpublic KangShiFuFour getKangShiFuFour(){getKangShiFuThree();return new KangShiFuFour();}
}

再写我们的测试类

package com.newcrud.learn;import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.annotations.Test;@SpringBootTest
public class MyConfigurationTwoTest extends AbstractTestNGSpringContextTests {@Testpublic void testGetKangShiFuThree() {//这个只是保留一个执行的入口,没有实际意义,我们要看的是启动信息}
}

在启动后会打印两个类的初始化函数输出

getKangShiFuThree
getKangShiFuFour

但是我们仔细想一下,我们是调用了两次getKangShiFuThree的构造函数的,但是只打印了一次,这也是他的一个功能:保持单例模式!

我们再来看一下他的另一个功能

@Configuration类允许通过调用同一类中的其他@Bean方法来定义bean之间的依赖关

package com.newcrud.learn;import org.springframework.context.annotation.Bean;public class MyConfigurationTwo {@Beanpublic KangShiFuThree getKangShiFuThree(){return new KangShiFuThree();}@Beanpublic KangShiFuFour getKangShiFuFour(){getKangShiFuThree();//去掉@Configuration,这里就会报错:Method annotated with @Bean is called directly. Use dependency injection instead.,因为未添加@Configuration注解,导致@Bean之间相互调用出错return new KangShiFuFour();}
}

http://www.yayakq.cn/news/351965/

相关文章:

  • 如何做淘外网站推广济南mip网站建设公司
  • 如何做kindle电子书下载网站wordpress主题森林 终身
  • seo百家外链网站人工智能培训机构排名前十
  • 门图书馆户网站建设方案广州 骏域网站建设 陶瓷
  • 织梦怎么制作网站微网站建设找哪家
  • wap建站模板旅游网站制作教程
  • wordpress类似网站模板网页加速器推荐
  • 梅州公司做网站王野天天
  • 北京网站建设搜q.479185700网站推广系统
  • 打车网站开发如何建设网站推广平台
  • 山东建设监理协会网站网站建设最好用什么语言
  • 网站源码 酷h5短视频源码
  • 网站建设网络宣传游戏网站如何做
  • 有哪些网站做二手房好的建设系统网站
  • 网站开发协议书由谁来写普通网站建设的缺陷
  • 网站建设实训报告心得体会企业信息管理系统免费
  • 广州网站建设 领航科技苏州网站制作开发公司
  • 直播间 网站建设十大app排行榜
  • 六安做网站多少钱专业定制网页设计
  • 如何做网站充值接口设计企业网站内容
  • 文昌建设局网站长沙做网站建设公司哪家好
  • 网页版梦幻西游红拂女安卓aso优化排名
  • 安庆市重点工程建设局网站网站响应速度多少合适
  • 易语言做网站爆破工具电子商务的网站建设分析
  • vr成品网站源码在线观看微商货源网站源码
  • 建设网站用什么空间服务器商城全网推广运营公司
  • 建设部网站施工合同范本html转pdf在线
  • 广州网站开发哪家强注册公司流程及步骤
  • 视频网站自己怎么做义乌购app下载
  • 建站公司哪家好 知道万维科技做网站 乐清