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

网站被百度蜘蛛爬了多久放出来怎么做电商创业

网站被百度蜘蛛爬了多久放出来,怎么做电商创业,专业的赣州网站建设,一个简单的网站搭建教程目录 1.Spring IoC&DI 2.关于Bean存储的相关注解(类注解与方法注解) Bean的获取方式 类注解和方法注解的重命名 2.1 类注解 2.1.1 Controller 2.1.2 Service 2.1.3 Repository 2.1.4 Component 2.1.5 Configuration 2.2 方法注解-Bean 2.2.1 定义多个对象 2.2…

目录

1.Spring IoC&DI

2.关于Bean存储的相关注解(类注解与方法注解)

Bean的获取方式

类注解和方法注解的重命名

2.1 类注解

2.1.1 @Controller

2.1.2 @Service

2.1.3 @Repository

2.1.4 @Component

2.1.5 @Configuration

2.2 方法注解-@Bean

2.2.1 定义多个对象

2.2.2 扫描路径-@ComponentScan

3.DI注入方式-@Autowired

3.1 属性注入

3.2 Setter方法注入

3.3 构造方法注入

3.4 三种注入方式的优缺点

3.5 @Autowired存在的问题及解决方法


1.Spring IoC&DI

Spring是包含了众多工具方法的一个IoC容器

  • IoC(Inversion of Control,控制反转),是Spring的一个核心思想,简单来说就是将对象之间层层的依赖关系反转过来(一开始是使用方创建并控制依赖对象,现在是把依赖对象注入到当前对象中),使得依赖对象无论发生什么改变,当前类都不受影响,大大降低了代码的耦合度
  • IoC容器就是创建并管理这些对象的容器,不再需要用户自己去创建对象,而是交给IoC容器去创建并对对象进行统一管理(将对象的控制权交给Spring)
  • DI(Dependency  Injection,依赖注入),是IoC的一种具体实现,依赖注入是一个过程,在Ioc容器在创建Bean时,去提供运行时所依赖的资源,这个资源就是对象,简单点说,依赖注入就是把对象取出来放到某个类的属性中

2.关于Bean存储的相关注解(类注解与方法注解)

Bean:Spring是一个IoC容器,而IoC容器中装的就是Bean(对象)

Bean的获取方式

Bean的获取方式有很多,常用的是以下三种

Object getBean(String name) throws BeansException根据Bean名称获取Bean
<T> T getBean(String name, Class<T> requiredType) throws BeansException根据Bean名称和类型获取Bean
<T> T getBean(Class<T> requiredType) throws BeansException根据类型获取Bean

Bean名称:当使用的是类注解时,Bean的名称为类名的小驼峰(第一个单词以小写字母开始,后面每个单词首字母大写),如果类名前两个字母都是大写,则Bean名称为类名;当使用的是方法注解时,Bean名称就是方法名

注:对同一个类获取多次Bean,指向的是同一个对象(类似单例模式,有且仅有一个对象)

类注解和方法注解的重命名

类注解重命名:类注解+(新名称)

//启动类代码
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
@SpringBootApplication
public class Demo3Application {public static void main(String[] args) {ApplicationContext context=SpringApplication.run(Demo3Application.class, args);UserComponent userComponent=(UserComponent) context.getBean("user1");//获取名称为user1的Bean//如果再去获取原来名称为userComponent的Bean,会报错userComponent.sayHi();}
}//UserComponent类
package com.example.demo;
import org.springframework.stereotype.Component;
@Component("user1")//重命名为user1
public class UserComponent {public void sayHi(){System.out.println("Hi, I'm UserComponent");}
}

方法注解重命名:@Bean+(新名称)

//启动类代码
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
@SpringBootApplication
public class Demo3Application {public static void main(String[] args) {ApplicationContext context=SpringApplication.run(Demo3Application.class, args);UserInfo userInfo1=(UserInfo) context.getBean("user1");UserInfo userInfo2=(UserInfo) context.getBean("user2");System.out.println(userInfo1);System.out.println(userInfo2);}
}//BeanTest类
package com.example.demo;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
@Component
public class BeanTest {@Bean("user1")//重命名为user1public UserInfo userInfo1(){return new UserInfo("zhangsan",1);}@Bean("user2")//重命名为user2public UserInfo userInfo2(){return new UserInfo("lisi",2);}
}

注:类注解和方法注解的重命名的名称可以有多个, 使用{ }来标识,例如@Component({"user1","user2"})

2.1 类注解

类注解总共有五种,这些注解都有各自不同的应用场景

  • @Controller:控制层(Controller),接收请求,对请求进行处理并进行响应
  • @Service:业务逻辑层(Service),处理具体的业务逻辑
  • @Repository:数据访问层(Dao),负责数据访问操作
  • @Configuration:配置层,处理项目中的一些配置信息
  • @Component:元注解,上述四种注解都含有@Component

2.1.1 @Controller

//启动类代码(从Spring中获取Bean)
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
@SpringBootApplication
public class Demo3Application {public static void main(String[] args) {ApplicationContext context=SpringApplication.run(Demo3Application.class, args);UserController userController=(UserController) context.getBean("userController");userController.sayHi();}
}//UserController类
package com.example.demo;
import org.springframework.stereotype.Controller;
@Controller//将对象存储到Spring中
public class UserController {public void sayHi(){System.out.println("Hi,I'm UserController");}
}

2.1.2 @Service

//启动类代码(从Spring中获取Bean)
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
@SpringBootApplication
public class Demo3Application {public static void main(String[] args) {ApplicationContext context=SpringApplication.run(Demo3Application.class, args);UserService userService=(UserService) context.getBean("userService");userService.sayHi();}
}//UserService类
package com.example.demo;
import org.springframework.stereotype.Service;
@Service//将对象存储到Spring中
public class UserService {public void sayHi(){System.out.println("Hi, I'm UserService");}
}

2.1.3 @Repository

//启动类代码(从Spring中获取Bean)
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
@SpringBootApplication
public class Demo3Application {public static void main(String[] args) {ApplicationContext context=SpringApplication.run(Demo3Application.class, args);UserRepository userRepository=(UserRepository) context.getBean("userRepository");userRepository.sayHi();}
}//UserRepository类
package com.example.demo;
import org.springframework.stereotype.Repository;
@Repository//将对象存储到Spring中
public class UserRepository {public void sayHi(){System.out.println("Hi,I'm UserRepository");}
}

2.1.4 @Component

//启动类代码(从Spring中获取Bean)
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
@SpringBootApplication
public class Demo3Application {public static void main(String[] args) {ApplicationContext context=SpringApplication.run(Demo3Application.class, args);UserComponent userComponent=(UserComponent) context.getBean("userComponent");userComponent.sayHi();}
}//UserComponent类
package com.example.demo;
import org.springframework.stereotype.Component;
@Component//将对象存储到Spring中
public class UserComponent {public void sayHi(){System.out.println("Hi, I'm UserComponent");}
}

2.1.5 @Configuration

//启动类代码(从Spring中获取Bean)
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
@SpringBootApplication
public class Demo3Application {public static void main(String[] args) {ApplicationContext context=SpringApplication.run(Demo3Application.class, args);UserConfiguration userConfiguration=(UserConfiguration) context.getBean("userConfiguration");userConfiguration.sayHi();}
}//UserConfiguration类
package com.example.demo;
import org.springframework.context.annotation.Configuration;
@Configuration//将对象存储到Spring中
public class UserConfiguration {public void sayHi(){System.out.println("Hi,I'm UserConfiguration");}
}

2.2 方法注解-@Bean

当一个类需要多个对象,或者需要使用外部包里的类时,使用类注解无法满足需求,这时就需要用到方法注解@Bean

2.2.1 定义多个对象

//启动类代码(从Spring中获取Bean)
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
@SpringBootApplication
public class Demo3Application {public static void main(String[] args) {ApplicationContext context=SpringApplication.run(Demo3Application.class, args);//如果这里使用类名获取Bean,会由于得到多个Bean而报错,所以需要使用上述Bean获取方式中的第1或第2种UserInfo userInfo1=(UserInfo) context.getBean("userInfo1");//获取方法名为userInfo1对应的BeanUserInfo userInfo2=(UserInfo) context.getBean("userInfo2");//获取方法名为userInfo2对应的BeanSystem.out.println(userInfo1);System.out.println(userInfo2);}
}//BeanTest类
package com.example.demo;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
@Component//方法注解需要搭配类注解一起使用,否则会报错
public class BeanTest {@Beanpublic UserInfo userInfo1(){return new UserInfo("zhangsan",1);}@Beanpublic UserInfo userInfo2(){return new UserInfo("lisi",2);}
}

2.2.2 扫描路径-@ComponentScan

Spring的扫描路径默认为启动类(@SpringBootApplication注释的类)所在的路径,如果路径下有被类注解注释的类,就会将该类的对象存储到Spring中。如果想要修改默认路径,可以通过@ComponentScan来指定扫描路径

//添加到启动类中,路径要和默认路径不同
@ComponentScan("com.example.demo.test")
//指定单个扫描路径
@ComponentScan({"com.example.demo.test","com.example.demo.test1"})
//指定多个扫描路径

3.DI注入方式-@Autowired

@Autowired注释的作用主要是从Spring中获取对象,注入到对象的使用方(根据类型注入)。对于DI注入,Spring提供了三种方式,分别是属性注入,Setter方法注入以及构造方法注入

3.1 属性注入

属性注入使用@Autowire实现,以下是使用属性注入实现Service类注入到Controller类的一个简单例子

//启动类代码(获取UserController对象中,调用sayHi方法)
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
@SpringBootApplication
public class Demo3Application {public static void main(String[] args) {ApplicationContext context=SpringApplication.run(Demo3Application.class, args);UserController userController=(UserController) context.getBean("userController");userController.sayHi();}
}//UserController类
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
@Controller
public class UserController {//使用属性注入将Service类注入到Controller类中@Autowiredprivate UserService userService;public void sayHi(){System.out.println("Hi,I'm UserController");userService.sayHi();}
}//UserService类
package com.example.demo;
import org.springframework.stereotype.Service;
@Service
public class UserService {public void sayHi(){System.out.println("Hi, I'm UserService");}
}

3.2 Setter方法注入

Setter方法注入即在设置set方法时加上@Autowired注解

//UserController类
//启动类代码和UserService类与属性注入中一致
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
@Controller
public class UserController {private UserService userService;//使用Setter方法注入将Service类注入到Controller类中@Autowiredpublic void setUserService(UserService userService) {this.userService = userService;}public void sayHi(){System.out.println("Hi,I'm UserController");userService.sayHi();}
}

3.3 构造方法注入

构造方法注入即在类的构造方法中实现注入

注:使用构造方法注入时,如果注入的类只有一个构造方法,那么@Autowired可以省略,如果注入的类有多个构造方法,那么需要添加@Autowired来明确指定使用哪个构造方法,否则会报错

//UserController类
//启动类代码和UserService类与属性注入中一致
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
@Controller
public class UserController {private UserService userService;//使用构造方法注入将Service类注入到Controller类中@Autowiredpublic UserController(UserService userService) {this.userService = userService;}public void sayHi(){System.out.println("Hi,I'm UserController");userService.sayHi();}
}

3.4 三种注入方式的优缺点

属性注入

  • 优点:简介,方便使用
  • 缺点:只能用于Ioc容器;不能注入一个Final修饰的属性

Setter方法注入

  • 优点:方便在类实例之后,重新对该对象进行配置或者注入
  • 缺点:不能注入一个Final修饰的属性;注入对象可能会被改变

构造方法注入

  • 优点:可以注入Final修饰的属性;注入的对象不会被修改;依赖对象被使用前一定会被完全初始化;通用性好,更换任何框架都可适用
  • 缺点:注入多个对象时,代码比较繁琐

3.5 @Autowired存在的问题及解决方法

当同一个类型存在多个Bean时,使用@Autowired会存在问题

//启动类代码
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
@SpringBootApplication
public class Demo3Application {public static void main(String[] args) {ApplicationContext context=SpringApplication.run(Demo3Application.class, args);UserController userController=context.getBean(UserController.class);userController.sayHi();}
}//UserController类
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
@Controller
public class UserController {@Autowired//注入UserInfoprivate UserInfo userInfo;public void sayHi(){System.out.println("Hi,I'm UserController");System.out.println(userInfo);}
}//BeanTest类
package com.example.demo;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
@Component
public class BeanTest {@Beanpublic UserInfo userInfo1(){return new UserInfo("zhangsan",1);}@Beanpublic UserInfo userInfo2(){return new UserInfo("lisi",2);}
}

@Autowired注解会根据类型UserInfo去查找Bean,找到userInfo1和userInfo2两个方法,由于查找到的Bean只能是唯一的,所以程序会报错(非唯一的Bean)。那么如何解决这个问题呢?Spring提供了几个解决方法:@Primary,@Qualifier,@Resource

@Primary:当同个类型存在多个Bean注入时,添加@Primary注解用来确定默认的实现

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
@Component
public class BeanTest {@Primary//指定该Bean为Bean的默认实现@Beanpublic UserInfo userInfo1(){return new UserInfo("zhangsan",1);}@Beanpublic UserInfo userInfo2(){return new UserInfo("lisi",2);}
}

@Qualifier:添加@Qualifier(Bean的名称),指定当前要注入的Bean(@Qualifier需要搭配@Autowired使用,不能单独使用)

package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
@Controller
public class UserController {@Qualifier("userInfo1")//指定Bean名称为userInfo1@Autowiredprivate UserInfo userInfo;public void sayHi(){System.out.println("Hi,I'm UserController");System.out.println(userInfo);}
}

@Resource:与@Qualifier用法相同,也是指定Bean的名称进行注入,但不需要搭配@Autowired

package com.example.demo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
@Controller
public class UserController {@Resource(name="userInfo1")//指定Bean名称为userInfo1并注入private UserInfo userInfo;public void sayHi(){System.out.println("Hi,I'm UserController");System.out.println(userInfo);}
}

除了上述三种使用注解的解决方法,还可以通过改变对象名称,使其与要注入的Bean的名称相同

package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
@Controller
public class UserController {@Autowiredprivate UserInfo userInfo1;//注入Bean名称为userInfo1,则对象名称也为userInfo1public void sayHi(){System.out.println("Hi,I'm UserController");System.out.println(userInfo1);}
}

文章转载自:
http://beguiler.wqfj.cn
http://hibernate.wqfj.cn
http://lacquer.wqfj.cn
http://numberless.wqfj.cn
http://haematimeter.wqfj.cn
http://vouchee.wqfj.cn
http://fluidity.wqfj.cn
http://panegyrize.wqfj.cn
http://overland.wqfj.cn
http://dumfriesshire.wqfj.cn
http://rocaille.wqfj.cn
http://viscometer.wqfj.cn
http://honeymoon.wqfj.cn
http://vespine.wqfj.cn
http://cryptanalysis.wqfj.cn
http://equilibria.wqfj.cn
http://phonetics.wqfj.cn
http://unquestionably.wqfj.cn
http://astringe.wqfj.cn
http://athwart.wqfj.cn
http://plug.wqfj.cn
http://oona.wqfj.cn
http://electrothermics.wqfj.cn
http://piedfort.wqfj.cn
http://jacobean.wqfj.cn
http://superbly.wqfj.cn
http://virescent.wqfj.cn
http://entironment.wqfj.cn
http://homocyclic.wqfj.cn
http://grassless.wqfj.cn
http://allopolyploidy.wqfj.cn
http://epiglottal.wqfj.cn
http://instant.wqfj.cn
http://unclog.wqfj.cn
http://negotiability.wqfj.cn
http://prudhoe.wqfj.cn
http://cubage.wqfj.cn
http://objectively.wqfj.cn
http://picaro.wqfj.cn
http://hyperplastic.wqfj.cn
http://wenny.wqfj.cn
http://cervicovaginal.wqfj.cn
http://lockmaking.wqfj.cn
http://dependable.wqfj.cn
http://tech.wqfj.cn
http://transference.wqfj.cn
http://pithily.wqfj.cn
http://expostulate.wqfj.cn
http://radian.wqfj.cn
http://exequatur.wqfj.cn
http://noncooperation.wqfj.cn
http://anisotropism.wqfj.cn
http://beetleweed.wqfj.cn
http://saltation.wqfj.cn
http://impassioned.wqfj.cn
http://carlowitz.wqfj.cn
http://effusive.wqfj.cn
http://tallith.wqfj.cn
http://striking.wqfj.cn
http://knobkerrie.wqfj.cn
http://masscult.wqfj.cn
http://enterobactin.wqfj.cn
http://honeycomb.wqfj.cn
http://trepanner.wqfj.cn
http://opposability.wqfj.cn
http://alban.wqfj.cn
http://timekeeper.wqfj.cn
http://empolder.wqfj.cn
http://epibolic.wqfj.cn
http://fivescore.wqfj.cn
http://mbone.wqfj.cn
http://comprizal.wqfj.cn
http://acetylide.wqfj.cn
http://beaune.wqfj.cn
http://foredeck.wqfj.cn
http://sporogeny.wqfj.cn
http://butane.wqfj.cn
http://vlan.wqfj.cn
http://cunner.wqfj.cn
http://reforge.wqfj.cn
http://glimmery.wqfj.cn
http://honeylipped.wqfj.cn
http://loxodont.wqfj.cn
http://eyewater.wqfj.cn
http://cuddie.wqfj.cn
http://hereditarily.wqfj.cn
http://megasporangium.wqfj.cn
http://nictheroy.wqfj.cn
http://cardiocirculatory.wqfj.cn
http://inconformable.wqfj.cn
http://douro.wqfj.cn
http://periscopical.wqfj.cn
http://berwick.wqfj.cn
http://ungodliness.wqfj.cn
http://v.wqfj.cn
http://dubiously.wqfj.cn
http://dolphinarium.wqfj.cn
http://tahina.wqfj.cn
http://screwloose.wqfj.cn
http://silicula.wqfj.cn
http://www.hrbkazy.com/news/61547.html

相关文章:

  • 网站设计导航栏怎么做无锡网站建设seo
  • 中文绿色环保网站模板广州市疫情最新
  • 深圳公司注册下来有哪些资料西安百度首页优化
  • 桂林户外论坛搜索引擎优化的工具
  • 建设主题网站一般要经历的顺序做营销策划的公司
  • 美食网站首页设计百度会员登录入口
  • 定制手机网站开发外链屏蔽逐步解除
  • 中企动力做网站真贵武汉seo优化公司
  • ctcms做的比较好的网站seo系统培训
  • python 快速做网站地推接单平台
  • 如何做一款服装网站百度地图关键词排名优化
  • 远程发布 wordpressseo网站排名优化服务
  • 网站建设公司专业的建站优化公司东莞网站优化
  • 网站建设现在什么服务器比较好深圳整站全网推广
  • 惠州网站制作公司哪家好新手做网络销售难吗
  • Godaddy优惠码网站怎么做的大数据培训
  • asp.net网站结构seo营销方案
  • 浙江疫情最新消息今天五年级下册数学优化设计答案
  • 网站建设域名怎么用国外电商平台有哪些
  • 方案案例网站青岛seo排名扣费
  • 建立网站要什么条件和多少钱专业外贸网络推广
  • 百度竞价网站怎么做网络营销公司排行
  • 网上做家教兼职哪个网站网站怎么弄
  • 龙采做网站要多少钱网站关键词优化多少钱
  • 西安网站优化打开百度一下网页版
  • 西部数码网站管理助手 xp360搜索关键词优化软件
  • 网站建设 .北京蓝纤湖南正规关键词优化报价
  • 淄博网站建设费用聊城今日头条最新
  • 本地网站建设杭州百度百家号seo优化排名
  • 如何用付费音乐做视频网站网址大全导航