Spring学习笔记之注解

一、Spring注解开发准备

1.导入jar包
commons-logging-1.2.jar
log4j-1.2.17.jar
spring-beans-5.0.4.RELEASE.jar
spring-context-5.0.4.RELEASE.jar
spring-core-5.0.4.RELEASE.jar
spring-expression-5.0.4.RELEASE.jar
2.创建类、创建方法
package com.jxs.di;

/**
 * Created by jiangxs on 2018/3/28.
 */
public class User {

    public void add() {

        System.out.println("this is add operation");
    }
}
3.创建Spring配置文件,引入约束

(1)在做IOC的基本功能时,引入了约束spring-beans
(2)在做Spring的IOC的注解开发时,除了引入约束beans外还需引入新约束spring-context

<?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">

</beans>
4.开启注解扫描
<?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">

 <!-- 开启注解扫描 -->
    <!-- 到包里面扫描类、方法、属性上面是否有注解 -->
    <!-- 如果要扫描jxs文件夹下的所有包,只需将base-package赋值为com.jxs即可 -->
    <context:component-scan base-package="com.jxs.di"></context:component-scan>

    <!-- 只扫描属性上的注解 -->
    <!--<context:annotation-config></context:annotation-config>-->

二、使用注解创建对象

1.在创建对象的类上面使用注解实现
package com.jxs.annotation;

import org.springframework.stereotype.Component;

/**
 * Created by jiangxs on 2018/3/28.
 */

@Component(value = "user") //效果等同于IOC的<bean id="user" class="com.jxs.xxx.User">
public class User {

    public void add() {

        System.out.println("this is add operation");
    }
}

测试代码:

package com.jxs.annotation;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by jiangxs on 2018/3/28.
 */
public class TestProgram {

    @Test
    public void testUser() {

        ApplicationContext context =
                new ClassPathXmlApplicationContext("bean1.xml");
        User user = (User) context.getBean("user");
        user.add();
    }
}

测试结果:

log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

this is add operation

Process finished with exit code 0
2.创建对象有四个注解

Spring中提供@Component的三个衍生注解:(功能目前来说一致)

  • @Controller:WEB层
  • @Service:业务层
  • @Respository:持久层
    @Component、@Controller、@Service和@Respository注解功能是一样的,都可以创建对象,Spring在后续版本会对其进行增强。
3.创建对象单实例还是多实例
@Service(value = "user")
@Scope(value = "prototype")
public class User {
...
}

设置value = “prototype”时创建对象时为多实例,不写value值时默认为单实例。

三、注解注入属性

1.注入属性第一个注解:@Autowired

(1)创建UserService类,创建UserDao类,创建userDao和userService对象,并在UserService类中定义dao类型属性,在UserService得到userDao对象

package com.jxs.annotation;

import org.springframework.stereotype.Component;

/**
 * Created by jiangxs on 2018/3/28.
 */
@Component(value = "userDao")
public class UserDao {

    public void add() {

        System.out.println("UserDao...this is add operation");
    }
}
package com.jxs.annotation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Created by jiangxs on 2018/3/28.
 */
@Component(value = "userService")
public class UserService {

    // 得到dao对象
    // 定义dao类型属性
    // 在dao属性上面使用注解完成对象的注入
    @Autowired
    private UserDao userDao;
    //使用注解方式的时候不需要set方法

    public void add() {

        System.out.println("UserService...this is add operation");
    }
}

测试程序:

package com.jxs.annotation;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by jiangxs on 2018/3/28.
 */
public class TestProgram {

    @Test
    public void testUserService() {

        ApplicationContext context =
                new ClassPathXmlApplicationContext("bean1.xml");
        UserService userService = (UserService) context.getBean("userService");
        userService.add();
    }
}

测试结果:

log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

UserService...this is add operation
UserDao...this is add operation

注意:自动装配与注入对象所在类中注入的value值无关。例如将UserDao.java中的value修改为:UserDao1234

/**
 * Created by jiangxs on 2018/3/28.
 */
@Component(value = "userDao")
public class UserDao {

    public void add() {

        System.out.println("UserDao...this is add operation");
    }
}

测试结果:

log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

UserService...this is add operation
UserDao...this is add operation

自动装配是自动寻找类名对应的对象。

2.第二个注解:@Resource
package com.jxs.annotation;

import org.springframework.stereotype.Component;

/**
 * Created by jiangxs on 2018/3/28.
 */
@Component(value = "userDao")
public class UserDao {

    public void add() {

        System.out.println("UserDao...this is add operation");
    }
}
package com.jxs.annotation;

import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * Created by jiangxs on 2018/3/28.
 */
@Component(value = "userService")
public class UserService {

    // 得到dao对象
    // 定义dao类型属性
    // 在dao属性上面使用注解完成对象的注入
    // name属性值写创建dao对象value值
    @Resource(name = "userDao")
    private UserDao userDao;
    //使用注解方式的时候不需要set方法

    public void add() {

        System.out.println("UserService...this is add operation");
        userDao.add();
    }
}

测试方法与测试结果与第一种方法相同:
测试程序:

package com.jxs.annotation;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by jiangxs on 2018/3/28.
 */
public class TestProgram {

    @Test
    public void testUserService() {

        ApplicationContext context =
                new ClassPathXmlApplicationContext("bean1.xml");
        UserService userService = (UserService) context.getBean("userService");
        userService.add();
    }
}

测试结果:

log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

UserService...this is add operation
UserDao...this is add operation

当name属性值与创建UserDao对象value值不相同时:

package com.jxs.annotation;

import org.springframework.stereotype.Component;

/**
 * Created by jiangxs on 2018/3/28.
 */
@Component(value = "userDao123")
public class UserDao {

    public void add() {

        System.out.println("UserDao...this is add operation");
    }
}

就会出现相关错误:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'userDao' available

Resource可以更加准确的指定要注入哪个对象

三、配置文件和注解混合使用

1.创建对象操作使用配置文件方式实现
<?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">

    <!-- 开启注解扫描 -->
    <!-- 到包里面扫描类、方法、属性上面是否有注解 -->
    <!-- 如果要扫描jxs文件夹下的所有包,只需将base-package赋值为com.jxs即可 -->
    <context:component-scan base-package="com.jxs.annotation"></context:component-scan>

    <bean id="bookDao" class="com.jxs.annotation.BookDao"></bean>
    <bean id="ordersDao" class="com.jxs.annotation.OrdersDao"></bean>
    <bean id="bookService" class="com.jxs.annotation.BookService"></bean>
</beans>
2.注入属性的操作使用注解的方式实现
package com.jxs.annotation;

/**
 * Created by jiangxs on 2018/3/28.
 */
public class BookDao {

    public void print() {

        System.out.println("this is BookDao");
    }
}
package com.jxs.annotation;

/**
 * Created by jiangxs on 2018/3/28.
 */
public class OrdersDao {

    public void print() {

        System.out.println("this is Ordersdao");
    }
}
package com.jxs.annotation;

import javax.annotation.Resource;

/**
 * Created by jiangxs on 2018/3/28.
 */
public class BookService {

    @Resource(name = "bookDao")
    private BookDao bookDao;
    @Resource(name = "ordersDao")
    private OrdersDao ordersDao;
    public void print() {

        bookDao.print();
        ordersDao.print();
        System.out.println("this is BookService");
    }

}

测试代码:

package com.jxs.annotation;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by jiangxs on 2018/3/28.
 */
public class TestProgram {

    @Test
    public void testBookService() {

        ApplicationContext context =
                new ClassPathXmlApplicationContext("bean2.xml");
        BookService bookService = (BookService) context.getBean("bookService");
        bookService.print();
    }
}

测试效果:

log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
this is BookDao
this is Ordersdao
this is BookService

Process finished with exit code 0
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REaDME.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值