Spring 的使用与配置
来源:互联网 发布:windows打印原理 编辑:程序博客网 时间:2024/05/29 04:43
理解 @Component, @Repository, @Controller, @Service
其实后三个本质上都是 @Component。
不过 @Repository 有一些多的功能,比如能把数据库的 Exception 转到更高层。
@Repository 可以通过 @Repository("name")
来标识一个 Bean, 十分好用。
装配到 Interface
Spring 在 @Autowired
时要装配 Interface 也不是具体的类。比如
@Repository("ss") public class StaffService implements DaoService<Staff>{ }
则装配时要装配到 Interface
@Autowired private DaoService<Staff> ss; // 不能用 private StaffService ss;
Spring XML 的配置
对于 Web 应用,要在 web.xml 中指定 spring.cfg.xml 的配置。
而对于普通应用,可通过 ClassPathXmlApplicationContext 来加载 IOC 容器的配置。
一个配置的例子
<?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" xmlns:security="http://www.springframework.org/schema/security" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 指定装配时, 可能的 Bean 包的位置 --> <context:component-scan base-package="com.jiayun.scp.controller" /><!-- ===========================Spring MVC config===========================--> <mvc:annotation-driven conversion-service="conversionService"/> <mvc:resources mapping="/static/**" location="/static/" /> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/" /> <property name="suffix" value=".jsp" /> </bean> <bean id="multipartResolver" class="org.springframework.web.multipart.support.StandardServletMultipartResolver" /><!-- Spring MVC Formatter config --> <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <property name="formatters"> <set> <bean class="com.jiayun.scp.formatter.LongToDateString" /> </set> </property> </bean><!--=================================== Spring Transaction Manager config===================================--><!-- Actually, spring hibernate5.LocalSessionFactory doesn't need a data source! It will use local hibernate configuration (hibernate.properties)<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost/jiayun_scp?autoReconnect=true&useUnicode=true&characterEncoding=utf-8" /> <property name="username" value="root" /> <property name="password" value="" /></bean>--> <tx:annotation-driven /> <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <!-- 这些属性在 Hibernate.proerties 中指定就可以了,不用在这里指定 <property name="dataSource" ref="dataSource"></property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop> <prop key="hibernate.connection.characterEncoding">UTF-8</prop> <prop key="hibernate.connection.useUnicode">true</prop> </props> </property> --> <!-- 专门指定 hibernate model 的映射 --> <property name="packagesToScan" value="com.jiayun.scp.model" /> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager" p:sessionFactory-ref="sessionFactory"> </bean> <!--=================================== spring security configuration===================================--> <bean id="encoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"> <constructor-arg name="strength" value="11" /> </bean> <security:http auto-config="true" use-expressions="true"> <security:intercept-url pattern="/sale/**" access="hasAnyRole('USER', 'ADMIN', 'SUPER')" /> <security:intercept-url pattern="/product/**" access="hasAnyRole('USER', 'ADMIN', 'SUPER')" /> <security:intercept-url pattern="/customer/**" access="hasAnyRole('USER', 'ADMIN', 'SUPER')" /> <security:intercept-url pattern="/staff/passwd/**" access="hasAnyRole('USER', 'ADMIN', 'SUPER')" /> <security:intercept-url pattern="/staff/**" access="hasAnyRole('ADMIN', 'SUPER')" /> <security:intercept-url pattern="/index**" access="hasAnyRole('USER', 'ADMIN', 'SUPER')"/> <security:csrf disabled="true"/> <security:form-login login-page="/login" default-target-url="/index" /> </security:http> <security:authentication-manager> <security:authentication-provider user-service-ref="jiayunUserDetailsService"> <!-- <security:password-encoder hash="plaintext"/> --> <security:password-encoder ref="encoder"/> </security:authentication-provider> </security:authentication-manager></beans>
0 0
- Spring 的使用与配置
- Spring 与 ActiveMq的集成配置使用
- Spring中事务的配置与使用
- Spring中bean的配置与使用
- spring 中 Spring-data-redis的配置与使用
- 基于标注的spring多数据源配置与使用
- Redis的java客户端(jedis)配置(spring)与使用
- Redis的java客户端(jedis)配置(spring)与使用
- Spring mvc中log4j的配置与使用
- spring mvc中log4j的配置与使用
- spring mvc中log4j的配置与使用
- spring mvc中log4j的配置与使用
- Redis的java客户端(jedis)配置(spring)与使用
- spring mvc中log4j的配置与使用
- spring mvc中log4j的配置与使用
- Spring quartz定时任务的配置与使用
- Spring Boot中缓存注解的配置与使用
- Spring与Hibernate的配置
- TOJ 2761.Buy One Get One Free
- 常用的刷新技术(一)——SwipeRefresh
- 【CF 583C】
- 4、Java 包及访问控制权限
- 《构建之法》读书笔记——第9章 项目经理
- Spring 的使用与配置
- 数据绑定
- 一个很详细的web.xml讲解
- C++ bitset类的使用与简介
- PageRank简单实现中的一个错误
- LVM_resize+根目录下各个目录的作用
- LeetCode(130) Surrounded Regions
- HashMap学习小记
- 扑克牌顺子