美容医疗 网站建设,四川建设教育网官网,设计医院网站建设,html网站前台模板spring使用注解开发 文章目录 1.前提1 Bean2 属性注入3 衍生的注解4.自动装配5 作用域 1.前提
步骤1#xff1a; 要使用注解开发#xff0c;就必须要保证AOP包的导入 步骤2#xff1a; xml文件添加context约束 步骤3#xff1a; 配置注解的支持 context:annotation-…spring使用注解开发 文章目录 1.前提1 Bean2 属性注入3 衍生的注解4.自动装配5 作用域 1.前提
步骤1 要使用注解开发就必须要保证AOP包的导入 步骤2 xml文件添加context约束 步骤3 配置注解的支持 context:annotation-config/
?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/beanshttps://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttps://www.springframework.org/schema/context/spring-context.xsd!-- 新增 --context:annotation-config/!-- 新增 --/beans步骤4 添加扫描包的支持context:component-scan base-packagecom.wq.pojo/ 指定要扫描的包这个包下的注解就会生效。
1 Bean Component 组件放在类上说明这个类被spring管理了就是Bean edge
package com.wq.pojo;import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;Component
public class User {Value(光头强)private String name;private int age;Overridepublic String toString() {return User{ name name \ , age age };}
}2 属性注入 Value 用于属性的注入相当于property namename value光头强/ 3 衍生的注解 Componet有几个衍生注解我们在web开发时会按照MVC三层架构分层。 DAO层 -- RepositoryService层 -- ServiceController层 -- Controller 这四个注解功能都是一样的代表将某个类注册到Spring中装配Bean 4.自动装配
1.Autowired Autowired 可以直接在属性上用 默认byType方式使用Autowired可以不用编写set方法如果自动装配的环境较为复杂自动装配无法通过一个Autowired注解完成的时候我们可以添加一个Qualifier(value dog1)注解来配合指定唯一的Bean对象注入 2.Resource Resource也能实现自动装配但不是spring的注解是jdk自带的注解jdk8后取消了 Autowired与Resource区别 都是用来自动装配的都可以放在属性字段上面。Autowired通过byType实现而且必须要求这个对象存在常用Resource默认通过byName方式实现如果找不到名字就会通过byType实现常用执行顺序不同 Autowired通过byType方式实现Resource默认通过byName方式实现 5 作用域
Scope(singleton) 放在类上