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

越野车网站模板网站设计论文分类号

越野车网站模板,网站设计论文分类号,常德网站制作公司,app定制开发公司在哪里目录 开启组件扫描 使用注解定义bean Autowired注入 场景一#xff1a;属性注入 场景二#xff1a;set注入 场景三#xff1a;构造方法注入 场景四#xff1a;形参注入 场景五#xff1a;只有一个构造函数#xff0c;无注解 场景六#xff1a;Autowired和Quali…目录 开启组件扫描 使用注解定义bean Autowired注入 场景一属性注入 场景二set注入 场景三构造方法注入 场景四形参注入 场景五只有一个构造函数无注解 场景六Autowired和Qualifier注解联合 Resource注入 场景一根据name注入 场景二name未知注入 场景三其他情况 Spring全注解开发 从 Java 5 开始Java 增加了对注解Annotation的支持它是代码中的一种特殊标记可以在编译、类加载和运行时被读取执行相应的处理。开发人员可以通过注解在不改变原有代码和逻辑的情况下在源代码中嵌入补充信息 Spring 从 2.5 版本开始提供了对注解技术的全面支持我们可以使用注解来实现自动装配简化 Spring 的 XML 配置 格式注解名称(属性值1属性值...) Spring 通过注解实现自动装配的步骤如下 引入依赖开启组件扫描使用注解定义 Bean依赖注入 子模块spring6-ioc-annotation 开启组件扫描 Spring 默认不使用注解装配 Bean因此我们需要在 Spring 的 XML 配置中通过 context:component-scan 元素开启 Spring Beans的自动扫描功能。开启此功能后Spring 会自动从扫描指定的包base-package 属性设置及其子包下的所有类如果类上使用了 Component 注解就将该类装配到容器中 ?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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd!--开启组件扫描功能--context:component-scan base-packagecom.qcby/context:component-scan /beans 注意在使用 context:component-scan 元素开启自动扫描功能前首先需要在 XML 配置的一级标签 beans 中添加 context 相关的约束 情况一最基本的扫描方式 context:component-scan base-packagecom.qcby/context:component-scan 情况二指定要排除的组件 context:component-scan base-packagecom.qcby!-- context:exclude-filter标签指定排除规则 --!--type设置排除或包含的依据typeannotation根据注解排除expression中设置要排除的注解的全类名typeassignable根据类型排除expression中设置要排除的类型的全类名--context:exclude-filter typeannotation expressionorg.springframework.stereotype.Controller/!--context:exclude-filter typeassignable expressioncom.qcby.controller.UserController/--/context:component-scan 情况三仅扫描指定组件 context:component-scan base-packagecom.qcby use-default-filtersfalse!-- context:include-filter标签指定在原有扫描规则的基础上追加的规则 --!-- use-default-filters属性取值false表示关闭默认扫描规则 --!-- 此时必须设置use-default-filtersfalse因为默认规则即扫描指定包下所有类 --!-- type设置排除或包含的依据typeannotation根据注解排除expression中设置要排除的注解的全类名typeassignable根据类型排除expression中设置要排除的类型的全类名--context:include-filter typeannotation expressionorg.springframework.stereotype.Controller/!--context:include-filter typeassignable expressioncom.qcby.controller.UserController/-- /context:component-scan 使用注解定义bean Spring 提供了以下多个注解这些注解可以直接标注在 Java 类上将它们定义成 Spring Bean 注解说明Component该注解用于描述 Spring 中的 Bean它是一个泛化的概念仅仅表示容器中的一个组件Bean并且可以作用在应用的任何层次例如 Service 层、Dao 层等。 使用时只需将该注解标注在相应类上即可Repository该注解用于将数据访问层Dao 层的类标识为 Spring 中的 Bean其功能与Component 相同Service该注解通常作用在业务层Service 层用于将业务层的类标识为 Spring 中的 Bean其功能与 Component 相同Controller该注解通常作用在控制层如SpringMVC 的 Controller用于将控制层的类标识为 Spring 中的 Bean其功能与 Component 相同 Autowired注入 单独使用Autowired注解默认根据类型装配【默认是byType】 该注解可以标注在哪里 构造方法、方法、形参、属性、注解 该注解有一个required属性默认值是true表示在注入的时候要求被注入的Bean必须是存在的如果不存在则报错。如果required属性设置为false表示注入的Bean存在或者不存在都没关系存在的话就注入不存在的话也不报错 场景一属性注入 package com.qcby.autowired.controller;import com.qcby.autowired.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller;Controller public class UserController {//注入service//第一种方式属性注入Autowired //根据类型找到对应对象完成注入private UserService userService;public void add(){System.out.println(controller...);userService.add();} }package com.qcby.autowired.service;public interface UserService {public void add(); }package com.qcby.autowired.service;import com.qcby.autowired.dao.UserDao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;Service public class UserServiceImpl implements UserService{//注入dao//第一种方式属性注入Autowiredprivate UserDao userDao;Overridepublic void add() {System.out.println(service...);userDao.add();} }package com.qcby.autowired.dao;public interface UserDao {public void add(); }package com.qcby.autowired.dao;import org.springframework.stereotype.Repository;Repository public class UserDaoImpl implements UserDao{Overridepublic void add() {System.out.println(dao...);} }测试 package com.qcby;import com.qcby.autowired.controller.UserController; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestUserController {public static void main(String[] args) {ApplicationContext context new ClassPathXmlApplicationContext(bean.xml);UserController controller context.getBean(UserController.class);controller.add();} }场景二set注入 package com.qcby.autowired.controller;import com.qcby.autowired.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller;Controller public class UserController {//第二种方式 set方法注入private UserService userService;Autowiredpublic void setUserService(UserService userService) {this.userService userService;}public void add(){System.out.println(controller...);userService.add();} }package com.qcby.autowired.service;import com.qcby.autowired.dao.UserDao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;Service public class UserServiceImpl implements UserService{//第二种方式 set方法注入private UserDao userDao;Autowiredpublic void setUserDao(UserDao userDao) {this.userDao userDao;}Overridepublic void add() {System.out.println(service...);userDao.add();} }场景三构造方法注入 package com.qcby.autowired.controller;import com.qcby.autowired.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller;Controller public class UserController {//第三种方式 构造方法注入private UserService userService;Autowiredpublic UserController(UserService userService) {this.userService userService;}public void add(){System.out.println(controller...);userService.add();} }package com.qcby.autowired.service;import com.qcby.autowired.dao.UserDao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;Service public class UserServiceImpl implements UserService{//第三种方式 构造方法注入private UserDao userDao;Autowiredpublic UserServiceImpl(UserDao userDao) {this.userDao userDao;}Overridepublic void add() {System.out.println(service...);userDao.add();} }场景四形参注入 package com.qcby.autowired.controller;import com.qcby.autowired.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller;Controller public class UserController {//第四种方式 形参注入private UserService userService;public UserController(Autowired UserService userService) {this.userService userService;}public void add(){System.out.println(controller...);userService.add();} }package com.qcby.autowired.service;import com.qcby.autowired.dao.UserDao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;Service public class UserServiceImpl implements UserService{//第四种方式 形参注入private UserDao userDao;public UserServiceImpl(Autowired UserDao userDao) {this.userDao userDao;}Overridepublic void add() {System.out.println(service...);userDao.add();} }场景五只有一个构造函数无注解 package com.qcby.autowired.controller;import com.qcby.autowired.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller;Controller public class UserController {//第五种方式 只有一个有参构造无注解private UserService userService;public UserController(UserService userService) {this.userService userService;}public void add(){System.out.println(controller...);userService.add();} }package com.qcby.autowired.service;import com.qcby.autowired.dao.UserDao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;Service public class UserServiceImpl implements UserService{//第五种方式 只有一个有参构造无注解private UserDao userDao;public UserServiceImpl(UserDao userDao) {this.userDao userDao;}Overridepublic void add() {System.out.println(service...);userDao.add();} }场景六Autowired和Qualifier注解联合 package com.qcby.autowired.dao;import org.springframework.stereotype.Repository;Repository public class UserRedisDaoImpl implements UserDao{Overridepublic void add() {System.out.println(dao redis...);} }package com.qcby.autowired.service;import com.qcby.autowired.dao.UserDao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service;Service public class UserServiceImpl implements UserService{//第六种方式 Autowired和Qualifier注解联合根据名称注入AutowiredQualifier(value userRedisDaoImpl)private UserDao userDao;Overridepublic void add() {System.out.println(service...);userDao.add();} }Resource注入 Resource注解也可以完成属性注入 Resource和Autowired的区别 Resource注解是JDK扩展包中的也就是说属于JDK的一部分。所以该注解是标准注解更加具有通用性Autowired注解是Spring框架自己的Resource注解默认根据名称装配byName未指定name时使用属性名作为name。通过name找不到的话会自动启动通过类型byType装配Autowired注解默认根据类型装配byType如果想根据名称装配需要配合Qualifier注解一起用Resource注解用在属性上、setter方法上Autowired注解用在属性上、setter方法上、构造方法上、构造方法参数上 场景一根据name注入 package com.qcby.resource.controller;import com.qcby.resource.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller;import javax.annotation.Resource;Controller public class UserController {//根据名称进行注入Resource(name myUserService)private UserService userService;public void add(){System.out.println(controller...);userService.add();} }场景二name未知注入 package com.qcby.resource.service;import com.qcby.resource.dao.UserDao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service;Service(value myUserService) public class UserServiceImpl implements UserService {//不指定名称根据属性名称进行注入private UserDao myUserDao;Overridepublic void add() {System.out.println(service...);myUserDao.add();} }场景三其他情况 package com.qcby.resource.controller;import com.qcby.resource.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller;import javax.annotation.Resource;Controller public class UserController {//根据类型配置Resourceprivate UserService userService;public void add(){System.out.println(controller...);userService.add();} }Spring全注解开发 全注解开发就是不再使用spring配置文件而是写一个配置类来代替配置文件 package com.qcby.autowired.config;import com.qcby.autowired.controller.UserController; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration;Configuration //配置类 ComponentScan(com.qcby) //开启组件扫描 public class SpringConfig {public static void main(String[] args) {//加载配置类ApplicationContext context new AnnotationConfigApplicationContext(SpringConfig.class);UserController controller context.getBean(UserController.class);controller.add();}}
http://www.w-s-a.com/news/703502/

相关文章:

  • windows网站建设教程视频教程wordpress默认用户头像
  • 做网站需要什么软件wordpress会员邮件通知
  • 技术支持网站合肥网站搭建
  • 无为网站设计免费制作企业网站平台
  • 社交网站第一步怎么做房屋装修效果图用什么软件
  • 企业网站 批量备案合肥 网站建设
  • 如何提高网站索引量室内设计师之路网站
  • ps怎么做响应式网站布局图现在做网站都是怎么做的
  • 导购 网站模板网站主题选择
  • 毕业设计医院网站设计怎么做郑州铭功路网站建设
  • 网站根域名是什么php做商城网站步骤
  • 建设网站的那个公司好网站建设万首先金手指12
  • 广东民航机场建设有限公司网站网站开发后端用什么
  • 做风帆网站需要多少钱越野车网站模板
  • 如何做网站平台销售用狗做头像的网站
  • 宝安电子厂做网站美食网页设计的制作过程
  • 网站logo提交学网站开发技术
  • 跨境电商平台网站建设广州西安官网seo推广
  • 我和你99谁做的网站小程序制作第三方平台
  • 建设银行网站用户名鹤岗网站seo
  • 做一元夺宝网站需要什么条件西安市做网站的公司
  • 零基础建设网站教程郑州做网站推广价格
  • 平面设计免费素材网站新开三端互通传奇网站
  • ppt模板免费下载 素材医疗seo网站优化推广怎么样
  • 课程网站怎么做wordpress文章改背景色
  • 网络营销从网站建设开始卖汽车配件怎么做网站
  • 手机商城网站制作公司济南想建设网站
  • .net 建网站网站网站做员工犯法吗
  • 电子商务网站建设说课稿棕色网站设计
  • 怎么做律所的官方网站红塔网站制作