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

建设厅安全证考试报名在哪个网站网站搭建网站设置

建设厅安全证考试报名在哪个网站,网站搭建网站设置,自动点击器免费下载,一键生成广告图目录 Component的使用 依赖注解的使用 非自定义Bean的注解开发 Component的使用 基本Bean注解&#xff0c;主要是使用注解的方式替代原有的xml的<bean>标签及其标签属性的配置&#xff0c;使用Component注解替代<bean>标签中的id以及class属性&#xff0c;而对…

目录

@Component的使用

依赖注解的使用

非自定义Bean的注解开发


@Component的使用

基本Bean注解,主要是使用注解的方式替代原有的xml的<bean>标签及其标签属性的配置,使用@Component注解替代<bean>标签中的id以及class属性,而对于是否延迟加载或是Bean的作用域,则是其他注解

xml配置

注解

描述

<bean scope="">

@Scope

在类上或使用了@Bean标注的方法上,标注Bean的作用范围,取值为singleton或prototype

<bean lazy-init="">

@Lazy

在类上或使用了@Bean标注的方法上,标注Bean是否延迟加载,取值为true或false

<bean init-method="">

@PostConstruct

在方法上使用,标注Bean的实例化后执行的方法

<bean destroy-method="">

@PreDestroy

在方法上使用,标注Bean的销毁前执行方法

下面就是基于注解的测试案例

首先需要开启自动扫描注解功能,这个功能还是需要在XML文件配置的

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"><!--开启自动扫描--><context:component-scan base-package="com.zmt"/>
</beans>
@Component("userService")
@Lazy(true)//开启懒加载
@Scope("singleton")//单例模式
public class UserServiceImpl implements UserService {public UserServiceImpl() {System.out.println("UserService被构造");}@PostConstructprivate void init(){System.out.println("执行init方法");}@PreDestroyprivate void destroy(){System.out.println("执行销毁方法");}
}

测试代码 

public class Test {public static void main(String[] args) {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml");Object bean = context.getBean("userService");System.out.println(bean);context.close();}
}

执行结果如下

UserService被构造
执行init方法
com.zmt.service.impl.UserServiceImpl@45afc369
执行销毁方法

为了方便区分不同的业务层,@Component注解又衍生了三个注解

  • @Service:Component的派生注解,多添加在Service的实现类上
  • @Controller:Component的派生注解,多添加在Controller类上
  • @Repository:Component的派生注解,多添加在Dao实现类上

依赖注解的使用

Bean的依赖注入的注解,主要是替代xml中的<property>标签中的注入操作

<bean id="" class=""><property name="" ref=""/><property name="" value=""/>
</bean>

Spring提供的注解如下,用于Bean内部进行属性注入的

属性注入注解

描述

@Value

使用在字段或方法上,用于注入普通数据

@Autowired

使用在字段或方法上,用于根据类型(byType)注入引用数据

@Qualifier

使用在字段或方法上,结合@Autowired使用,根据名称注入

@Resource

使用在字段或方法上,根据类型或名称进行注入

这些注解的工作原理实际上是通过暴力反射然后赋值,因此不需要set方法,但是添加了set方法在set方法上添加注解也可以使用。

下面是简单的使用案例

@Value该注解可以对普通数据类型进行赋值,一种是直接指定需要注入的值,一种是通过占位符读取需要注入的值信息

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"><!--开启自动扫描--><context:component-scan base-package="com.zmt"/><!--将test.txt文件加载到Spring中,供赋值使用--><context:property-placeholder location="classpath:test.txt"/>
</beans>

@Component("userService")
public class UserServiceImpl implements UserService {
//    @Value("zhangsan")@Value("${name}")private String name;@Overridepublic void show() {System.out.println(name);}
}

以上两种都可以将值赋值给name变量,但前者使用的意义不大,因此通常我们使用后者去读取配置文件中的值。


@Autowired的使用默认是根据类型注入,但如果相同类型存在多个,则根据名称进行注入,但是如果不存在属性变量名的Bean对象,那么注入失败

@Component("userService")
public class UserServiceImpl implements UserService {@Autowiredprivate UserDao userDao;@Autowiredpublic void setUserDao(UserDao userDao) {this.userDao = userDao;}//以上两种写一个即可@Overridepublic void show() {}
}

@Qualifier需要搭配@Autowired注解使用,特定指定在多个相同类型的Bean对象时,注入哪个名称的bean对象

@Component("userService")
public class UserServiceImpl implements UserService {@Autowired@Qualifier("userDao")private UserDao userDao2;@Overridepublic void show() {}
}

这里即使属性变量名为userDao2但是实际上注入的还是名为userDao的bean对象。


@Resource既可以类型注入也可以名称注入,如果指定名称,那么只能根据名称注入

@Component("userService")
public class UserServiceImpl implements UserService {@Resource(name = "userDao")private UserDao userDao2;//实际上注入的是名称为userDao的bean对象@Overridepublic void show() {}
}

@Autowired的扩展使用,实际上该注解不仅仅可以添加在set方法上,可以添加在任何方法上,下面是一个测试案例

@Component("userService")
public class UserServiceImpl implements UserService {@Overridepublic void show() {}@Autowiredpublic void xxx(UserDao userDao){System.out.println("xxx:"+userDao);}@Autowiredpublic void yyy(List<UserDao> userDaoList){System.out.println("yyy:"+userDaoList);}
}

运行结果如下

非自定义Bean的注解开发

对于非自定义的Bean我们需要使用工厂来进行加载,需要使用@Bean注解,如果需要参数,对于基本类型需要@Value注解,对于引用类型,只要Spring中存在,可以直接根据类型注入。

@Component
public class OtherBean {@Beanpublic DataSource dataSource(@Value("${name}") String name){DataSource dataSource = new DataSource();System.out.println(name);return dataSource;}
}
http://www.yayakq.cn/news/743719/

相关文章:

  • 珠海网站策划公司苏州专业高端网站建设机构
  • 网站手机客户端开发教程抖音蓝号代运营
  • 花样云做网站怎样网站速度慢的原因
  • 24小时通过网站备案百度网页制作
  • 网页设计与网站开发的卷子网站建设软件dw
  • 国内网站免备案wordpress翻译po文件
  • 网页制作公司兼职seo经理招聘
  • seo站群优化技术做会员体系的网站
  • 电子商务网站建设试卷.doc做网站维护一工资多少钱
  • 搭建电商平台seo短视频网页入口营销策略
  • 企业网站功能是什么最好的wordpress主题
  • 怎么上传图片到公司网站站长工具无吗经典
  • 网站建设及编辑岗位职责石家庄h5网站建设
  • 网站名称价格网站制作需要哪些软件
  • 做网站服务器权限设置seo com
  • 起域名网站基于oa系统的网站建设
  • 用discuz做网站树莓派搭建wordpress卡不卡
  • 网站导航结构做网站的公司名称
  • 安徽方圆建设有限公司网站西安网站建设推广优化
  • 为企业设计一个网站中铁建设企业门户登录
  • 娄底网站建设网站云南seo公司
  • 吉林房地产网站开发百度推广还要求做网站
  • 北京网站设计工资多少wordpress+企业库插件
  • 济宁网站建设服务卓拙科技做网站吗
  • 深度网网站建设wordpress显示插件怎么用
  • 视频网站 外链微信网页版怎么扫描二维码
  • 网站建设都需要什么wordpress 增加中文
  • 网站建设腾讯云与阿里云用jsp实现网站开发的流程
  • 建设网站 宣传平台开发公司与物业公司合同
  • 公司网站建设步骤网站优化建设桂林