软件编程
位置:首页>> 软件编程>> java编程>> Spring注解与P/C命名空间超详细解析

Spring注解与P/C命名空间超详细解析

作者:wei_shuo  发布时间:2022-08-04 19:42:34 

标签:Spring,注解,P命名空间,C命名空间

注解实现自动装配

@Autowire注解

@Autowire注解,自动装配通过类型,名字如果Autowire不能唯一自动装配上属性,

则需要通过@Qualifier(value=“xxx”)

配置:

导入约束

<?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  
      http://www.springframework.org/schema/context/spring-context.xsd
       ">

开启注解支持

<context:annotation-config/>

实现:

Dog/Cat类和方法的实现还是不变

People类

  • 实现@Autowire注解、可以在属性上使用、也可以在set方式使用

  • 编写@Autowire注解后,set方法可以省略不写

public class People {
   @Autowired
   private Cat cat;
   @Autowired
   private Dog dog;
   private String name;
}

beans.xml

<bean id="cat" class="com.wei.pojo.Cat"/>
   <bean id="dog" class="com.wei.pojo.Dog"/>
   <bean id="people" class="com.wei.pojo.People"/>

@Nullable 字段标记了这个注解,说明这个字段可以为null

可以在带参构造中使用@Nullable

public People(@Nullable String name) {
       this.name = name;
   }

@Qualifier注解

指定一个唯一的bean对象注入

People类

public class People {
   @Autowired
   @Qualifier(value = "cat111")
   private Cat cat;
   @Autowired
   @Qualifier(value = "dog222")
   private Dog dog;
   private String name;
}

beans.xml

<bean id="cat" class="com.wei.pojo.Cat"/>
   <bean id="cat111" class="com.wei.pojo.Cat"/>
   <bean id="dog" class="com.wei.pojo.Dog"/>
   <bean id="dog222" class="com.wei.pojo.Dog"/>
   <bean id="people" class="com.wei.pojo.People"/>

@Resource注解

默认按照名字装配Bean,即会按照name属性的值来找到具有相同id的Bean Definition 并注入

People类

public class People {
   @Resource(name = "cat2")
   private Cat cat;
   @Resource
   private Dog dog;
   private String name;
}

beans.xml

<bean id="cat1" class="com.wei.pojo.Cat"/>
<bean id="cat2" class="com.wei.pojo.Cat"/>
<bean id="dog" class="com.wei.pojo.Dog"/>
<bean id="people" class="com.wei.pojo.People"/>

总结:

  • @Autowire 通过byType方式实现,而且必须要求对象存在,不能为null

  • @Resource 默认通过byname的方式实现,如果找不到名字,则通过byType实现,都不行则报错

@Component

放在类上,说明这个类被Spring管理了,等同于bean配置

Java类

此处使用注解@Component @Value 则 beans.xml则无需书写bean

//等价于<bean id="user" class="com.wei.pojo.User       Component:组件
@Component
public class User {
   @Value("wei_shuo")  //相当于 <property name="name" value="wei_shuo"/>
   public String name;
}

@Component的衍生注解,在web开发中,按照MVC三层架构分层

四个注解功能、作用相同,都是代表将某个类注册到Spring中、装配Bean

  • pojo &mdash;> @Component

  • dao &mdash;> @Resource

  • service &mdash;> @Service

  • controller &mdash;> @Controller

@Scope

作用域注解,限定Spring Bean的作用范围,在Spring配置文件定义Bean时,通过声明scope配置项,可以灵活定义Bean的作用范围

@ComponentScan

@ComponentScan(&ldquo;com.wei.pojo&rdquo;) 扫描包

@Bean

  • 注册一个bean,就相当于bean标签

  • 方法的名字,相当于bean标签的id属性

  • 方法的返回值,相当于bean标签的class属性

@Bean
   public User getUser(){
       return new User();      //就是返回要注入到bean的对象
   }

@Configuration

@Configuration注解会被Spring容器托管,因为它本身就是一个@Component

@Configuration 代表这是一个配置类,相当于beans.xml

@Configuration
@ComponentScan("com.wei.pojo")      
@Import(WeiConfig2.class)          
public class WeiConfig {
   @Bean
   public User getUser(){
       return new User();      
   }
}

@Value

属性注入值

Java类

此处使用@Scope注解使用prototype多实例注解模式

//等价于<bean id="user" class="com.wei.pojo.User       Component:组件
@Component
@Scope("prototype")//作用域注解
public class User {
   @Value("wei_shuo")  //相当于 <property name="name" value="wei_shuo"/>
   public String name;
}

P命名空间注入

p命名空间注入,对应Set注入方式

需要创建set/get方法

p命名空间注入,可以直接注入属性的值:property

User类

package com.wei.pojo;
public class User {
   private String name;
   private int age;
   public String getName() {
       return name;
   }
   public void setName(String name) {
       this.name = name;
   }
   public int getAge() {
       return age;
   }
   public void setAge(int age) {
       this.age = age;
   }
   @Override
   public String toString() {
       return "User{" +
               "name='" + name + '\'' +
               ", age=" + age +
               '}';
   }
}

userbeans.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:p="http://www.springframework.org/schema/p"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--上面添加 xmlns:p="http://www.springframework.org/schema/p" -->
   <!--p命名空间注入,可以直接注入属性的值:property-->
   <bean id="user" class="com.wei.pojo.User" p:name="秦疆" p:age="18"/>
</beans>

Test测试

ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
//        指定类型User.class则不需要强制转换
       User user = context.getBean("user", User.class);
//        强制转换
//        User user = (User) context.getBean("user");
       System.out.println(user);

C命名空间注入

c命名空间注入,对应构造器注入

需要创建无参/带参构造方法

c命名空间注入,通过构造器 注入:construct-args

User类

此处增加无参/带参构造方法

public class User {
   private String name;
   private int age;
   public User() {
   }
   public User(String name, int age) {
       this.name = name;
       this.age = age;
   }
   public String getName() {
       return name;
   }
   public void setName(String name) {
       this.name = name;
   }
   public int getAge() {
       return age;
   }
   public void setAge(int age) {
       this.age = age;
   }
   @Override
   public String toString() {
       return "User{" +
               "name='" + name + '\'' +
               ", age=" + age +
               '}';
   }
}

userbeans.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:p="http://www.springframework.org/schema/p"
      xmlns:c="http://www.springframework.org/schema/c"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--上面添加  xmlns:c="http://www.springframework.org/schema/c" -->
   <!--c命名空间注入,通过构造器 注入:construct-args-->
   <bean id="user2" class="com.wei.pojo.User" c:name="wei_shuo" c:age="18"/>
</beans>

Test测试

ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
       User user = context.getBean("user2", User.class);
       System.out.println(user);

总结:

p命名空间注入/c命名空间注入 都需要导入xml约束,不能直接使用

xmlns:p=&ldquo;http://www.springframework.org/schema/p&rdquo;

xmlns:c=&ldquo;http://www.springframework.org/schema/c&rdquo;

Spring开发包名解释

dao包

数据库操作,crud 即增删改查,对于数据库的增删改查的操作都在这里

pojo包

简单java对象

service包

service 服务器层,也叫业务逻辑层,调用dao中的方法

Java方式配置

使用Java的方式配置Spring,也就是不写bean.xml配置,使用注解代替

实体列

//@Configurable放在类上,说明这个类被Spring管理了,等同于bean配置,注册到容器中
@Configurable
public class User {
   private String name;
   public String getName() {
       return name;
   }
   @Value("CSDN")  //属性注入值
   public void setName(String name) {
       this.name = name;
   }
   @Override
   public String toString() {
       return "User{" +
               "name='" + name + '\'' +
               '}';
   }
}

配置文件

  • @Configuration注解会被Spring容器托管,因为它本身就是一个@Component

  • @Configuration 代表这是一个配置类,相当于beans.xml

  • @Import注解,功能就是和Spring XML里面的一样. @Import注解是用来导入配置类或者一些需要前置加载的类.通俗的将就是将类放入到IOC容器中

  • @Bean注解,注册一个bean,就相当于bean标签,方法的名字,相当于bean标签的id属性,方法的返回值,相当于bean标签的class属性

//@Configuration注解会被Spring容器托管,因为它本身就是一个@Component
//@Configuration 代表这是一个配置类,相当于beans.xml
@Configuration
@ComponentScan("com.wei.pojo")      //扫描包
@Import(WeiConfig2.class)           //@Import注解,功能就是和Spring XML里面的一样. @Import注解是用来导入配置类或者一些需要前置加载的类.通俗的将就是将类放入到IOC容器中
public class WeiConfig {
   //注册一个bean,就相当于bean标签
   //方法的名字,相当于bean标签的id属性
   //方法的返回值,相当于bean标签的class属性
   @Bean
   public User getUser(){
       return new User();      //就是返回要注入到bean的对象
   }
}

测试类

public class MyTest {
   //如果完全使用了配置类方式去做,只能通过AnnotationConfig 上下文来获取容器,通过配置类的class对象加载
   public static void main(String[] args) {
       ApplicationContext context = new AnnotationConfigApplicationContext(WeiConfig.class);
       User getUser = (User) context.getBean("getUser");   //此处取方法名
       System.out.println(getUser.getName());
   }
}

来源:https://weishuo.blog.csdn.net/article/details/127456652

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com