设为首页 加入收藏

TOP

SSH框架之Spring第二篇(一)
2019-09-23 11:13:09 】 浏览:153
Tags:SSH 框架 Spring 第二篇
1.1 基于注解的IOC配置
        既注解配置和xml配置要实现的功能都是一样的,都是要降低程序间的耦合.只是配置的形式不一样.
    
    1.2 环境搭建
        1.2.1 第一步:拷贝必备的jar包
        需要多拷贝一个spring-aop-4.2.4.RELEASE.jar
        1.2.2 创建xml文件,导入约束
            <?xml version="1.0" encoding="UTF-8"?>
            <!-- 导入schema 
            约束的位置在:
                ..\spring-framework-4.2.4.RELEASE\docs\spring-framework-reference\html\xsd-configuration.html
                文件中。
            注意:要导入schema约束
            -->
            <beans xmlns="http://www.springframework.org/schema/beans"
                  xmlns:context="http://www.springframework.org/schema/context"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   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>
        1.2.3 使用@Component注解配置管理的资源
            /**
             * 客户的业务层实现类
             * @author zhy
             * 
            @Component(value="customerService")
            public class CustomerServiceImpl implements ICustomerService {
                @Override
                public void saveCustomer() {
                    System.out.println("执行了保存客户");
                }
            }
        1.2.4 第四步在spring的配置文件中开启spring对注解ioc的支持
            <!--告知spring框架在,读取配置文件,创建容器时,扫描注解,依据注解创建对象,并存入容器中.>
                <context:component-scan base-package="com.baidu"></context:component-sacn>
    1.3 常用注解
        1.3.1 用于创建对象的
            相当于 : <bean id="" class="">
        1.3.1.1 @Component
            作用:
                把资源让spring来管理.相当于在xml配置一个bean.
            属性:
                value: 指定bean的id.如果不指定value属性,默认bean的id是当前类的类名.首字母小写.
        1.31.2 @Controller @Service @Repository
            们三个注解都是针对一个的衍生注解,他们的作用及属性都是一模一样的。
            他们只不过是提供了更加明确的语义化。
                @Controller:一般用于表现层的注解。
                @Service:一般用于业务层的注解。
                @Repository:一般用于持久层的注解。
            细节:如果注解中有且只有一个属性要赋值时,且名称是value,value在赋值是可以不写。
        1.3.2 用于注入数据的
            相当于 : <property name="" ref="">  或者 <property name="" value="">
            1.3.2.1@Autowired
            作用:
                自动按照类型注入。当使用注解注入属性时,set方法可以省略。它只能注入其他bean类型。当有多个类型匹配时,使用要注入的对象变量名称作为bean的id,在spring容器查找,找到了也可以注入成功。找不到就报错。
                /**
                 *     @Autowired    自动装配,自动按类型注入对象
                 *     <bean id="userService" class="com.baidu.demo2.UserServiceImpl" scope="" init-method="init">
                 *         <property name="userDao" ref="ud"/>
                 *     </bean>
                 *     
                 *     @Autowired
                    @Qualifier(value="userDao")    这2个注解必须要一起使用,按id名称注入
                    
                    @Resource    是Java提供注解,Spring容器支持该注解。可以通过name在容器中查找指定名称的对象
                 *     
            1.3.2.2@Qualifier
            作用:
                在自动按照类型注入的基础之上,再按照Bean的id注入。它在给字段注入时不能独立使用,必须和@Autowire一起使用;但是给方法参数注入时,可以独立使用。
            属性:
                value:指定bean的id。
            1.3.2.3@Resource
            作用:
                直接按照Bean的id注入。它也只能注入其他bean类型。
            属性:
                name:指定bean的id。
            1.3.2.4@Value
            作用:
                注入基本数据类型和String类型数据的
            属性:
                value:用于指定值
            1.3.3用于改变作用范围的:
                相当于:<bean id="" class="" scope="">
            1.3.3.1@Scope
            作用:
                指定bean的作用范围。
            属性:
                value:指定范围的值。
                       取值:singleton  prototype request session globalsession
            1.3.4和生命周期相关的:(了解)  
                相当于:<bean id="" class="" init-method="" destroy-method="" />
            1.3.4.1@PostConstruct
            作用:
                用于指定初始化方法。
            1.3.4.2@PreDestroy
            作用:
                用于指定销毁方法。
            1.3.5代码示例
            业务层
首页 上一页 1 2 3 4 下一页 尾页 1/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇01.Django基础一之web框架的本质 下一篇SSH框架之Spring第三篇

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目