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

软文网站推广法wordpress特别卡 iis

软文网站推广法,wordpress特别卡 iis,三合一网站建设官网,凡科互动游戏修改器目录 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 version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://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-packagecom.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.UserServiceImpl45afc369 执行销毁方法 为了方便区分不同的业务层Component注解又衍生了三个注解 ServiceComponent的派生注解多添加在Service的实现类上ControllerComponent的派生注解多添加在Controller类上RepositoryComponent的派生注解多添加在Dao实现类上 依赖注解的使用 Bean的依赖注入的注解主要是替代xml中的property标签中的注入操作 bean id classproperty name ref/property name value/ /bean Spring提供的注解如下用于Bean内部进行属性注入的 属性注入注解 描述 Value 使用在字段或方法上用于注入普通数据 Autowired 使用在字段或方法上用于根据类型(byType)注入引用数据 Qualifier 使用在字段或方法上结合Autowired使用根据名称注入 Resource 使用在字段或方法上根据类型或名称进行注入 这些注解的工作原理实际上是通过暴力反射然后赋值因此不需要set方法但是添加了set方法在set方法上添加注解也可以使用。 下面是简单的使用案例 Value该注解可以对普通数据类型进行赋值一种是直接指定需要注入的值一种是通过占位符读取需要注入的值信息 ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://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-packagecom.zmt/!--将test.txt文件加载到Spring中供赋值使用--context:property-placeholder locationclasspath: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 {AutowiredQualifier(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(ListUserDao 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.zqtcl.cn/news/755199/

相关文章:

  • 高端网站建设费用情况广州开发区控股集团有限公司
  • 精湛的网站设计云南网招聘
  • 南昌网站建设公司行情Wordpress添加分页按钮
  • 论坛网站建设流程wordpress速度优化插件
  • PHP套模板做网站建设银行保定分行网站
  • 怎样免费注册网站域名wordpress网站回调域
  • 东莞个人免费建网站乐清网约车事件
  • 备案查询网站网站的登录弹窗怎么做
  • 网站开发 mvc北京建设工程主管部门网站
  • 淮安建设机械网站制作代理公司注册需要多少钱
  • 站长收录茌平建设局网站
  • 如何进行网站开发开发区人才
  • 网站制作 视频网站维护的主要内容包括
  • 快速企业建站深圳网站关键词优化推广
  • 如何开网店详细步骤东莞市网络seo推广
  • 个人可以做哪些有意思的网站网站和网站的app
  • 北京高端网站开发公司网站建设后台实训体会
  • 青岛海川建设集团有限公司网站wordpress 变私有云
  • 网站备案人可以改吗石大网页设计与网站建设客观题
  • 宁波网站优化方案免费关键词挖掘工具
  • 网站制作想法免费做效果图网站
  • 晓风彩票网站建设软件微信上发的链接网站怎么做的
  • 关键词有哪几种台州优秀关键词优化
  • 盐田区住房和建设局网站软件开发文档怎么编写
  • 网站响应式建设seo排名优化怎样
  • 山东 网站备案德清县建设局网站
  • 中英语双语网站咋做提供网站建设设计外包
  • 云网站功能江门网站seo关键词排名优化
  • 潍坊网站建设外贸制作html网站
  • 网站友情链接怎么添加定制酒营销方案