zoukankan      html  css  js  c++  java
  • spring入门(二) 使用注解代替xml配置

    1.导包(略)

    2.applicationContext.xml如下:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4        xmlns:context="http://www.springframework.org/schema/context"
     5        xsi:schemaLocation="http://www.springframework.org/schema/beans
     6         http://www.springframework.org/schema/beans/spring-beans.xsd
     7         http://www.springframework.org/schema/context
     8         http://www.springframework.org/schema/context/spring-context.xsd
     9         ">
    10     <!--注意配置xsd,要么报错:通配符的匹配很全面, 但无法找到元素 'context:component-scan' 的声明。-->
    11 
    12     <context:component-scan base-package="com.ice.bean"/>
    13 </beans>
    base-package扫描的包,会包含子包.

    3.@Component

    写法一: User类

     1 import org.springframework.stereotype.Component;
     2 
     3 @Component(value = "user") //等同于在xml里定义了 <bean id="user" class="" />
     4 public class User {
     5     private String name;
     6     private Integer age;
     7 
     8     public String getName() {
     9         return name;
    10     }
    11 
    12     public void setName(String name) {
    13         this.name = name;
    14     }
    15 
    16     public Integer getAge() {
    17         return age;
    18     }
    19 
    20     public void setAge(Integer age) {
    21         this.age = age;
    22     }
    23 }

    main方法:

        ApplicationContext context = new ClassPathXmlApplicationContext("setting/applicationContext.xml");
        User user = (User) context.getBean("user");

    写法二: User类

     1 import org.springframework.stereotype.Component;
     2 
     3 @Component
     4 public class User {
     5     private String name;
     6     private Integer age;
     7 
     8     public String getName() {
     9         return name;
    10     }
    11 
    12     public void setName(String name) {
    13         this.name = name;
    14     }
    15 
    16     public Integer getAge() {
    17         return age;
    18     }
    19 
    20     public void setAge(Integer age) {
    21         this.age = age;
    22     }
    23 }

    main方法:

        ApplicationContext context = new ClassPathXmlApplicationContext("setting/applicationContext.xml");
        User user = context.getBean(User.class);

    4.其他注解

    @Repository
    @Service
    @Controller 这三个注解跟Component一个效果
    @Scope(scopeName = "prototype")//singleton prototype
    @Value 放在字段上,是反射实现赋值;放在set方法上,是通过调用方法赋值.

    @Autowired 按照类型 //import org.springframework.beans.factory.annotation.Autowired;
    @Qualifier 按照名称 //@Autowired @Qualifier 可以一起用
    @Resource //import javax.annotation.Resource;

    ...

  • 相关阅读:
    CentOS 7 配置 ISCSI 服务器
    CentOS 7 配置 http 服务器
    CentOS 7 配置 samba服务器
    ssh的两种连接方法(包括无密码访问)
    CentOS 7 破解mariadb密码
    Selenium2+python自动化53-unittest批量执行(discover)【转载】
    Selenium2+python自动化52-unittest执行顺序【转载】
    Selenium2+python自动化51-unittest简介【转载】
    selenium3+python自动化50-环境搭建(firefox)【转载】
    Selenium2+python自动化49-判断文本(text_to_be_present_in_element)【转载】
  • 原文地址:https://www.cnblogs.com/ICE_Inspire/p/9726955.html
Copyright © 2011-2022 走看看