spring 配置(Spring有几种配置方式 )

本文目录
Spring有几种配置方式
Spring有几种配置方式 ?
基于XML文件的配置 这种配置文件的格式常用《beans》开头,然后运用一系列的bean定义和专门的应用配置选项组成。 Spring XML配置方式是使用被Spring命名空间所支持的一些列XML的标签来实现的。
基于注解的配置 可以使用注解的方式来代替XML方式的bean元素的配置。这就是组件扫描,常用依赖注入的一些注解有: @Controller @Service @Autowired @RequestMapping @RequestParam @ModelAttribute @Cacheable @CacheFlush @Resource @PostConstruct @PreDestroy @Repository @Scope @SessionAttributes @InitBinder @Required @Qualifier
组件扫描: 容器会扫描base-package指定的包及其子包下面的所有类,如果该类有一些特定的注解,则纳入容器进行管理。
在类前面添加的一些特定的注解: @Component 通用注解 @Repository 持久层注解 @Service 业务层注解、 @Controller 控制层注解
基于Java的配置
springboot 配置多套环境
《id》dev
《env》dev
《jvmSettings.initialMemorySize》512
《jvmSettings.maxMemorySize》512
《jvmSettings.javaagent》-javaagent:/data/app/pinpoint-agent-1.8.3/pinpoint-bootstrap-1.8.3.jar
《activeByDefault》true
《id》uat1
《env》uat1
《jvmSettings.initialMemorySize》512
《jvmSettings.maxMemorySize》512
《jvmSettings.javaagent》-javaagent:/data/app/pinpoint-agent-1.8.3/pinpoint-bootstrap-1.8.3.jar
《id》stg
《env》stg
《jvmSettings.initialMemorySize》1024
《jvmSettings.maxMemorySize》1024
《jvmSettings.javaagent》-javaagent:/data/app/pinpoint-agent-1.8.3/pinpoint-bootstrap-1.8.3.jar
《id》prd
《env》prd
《jvmSettings.initialMemorySize》2048
《jvmSettings.maxMemorySize》2048
《jvmSettings.javaagent》-javaagent:/data/app/pinpoint-agent-1.8.3/pinpoint-bootstrap-1.8.3.jar
《directory》src/main/resources
《exclude》env/**
《directory》src/main/resources/env/${env}
《targetPath》${project.build.outputDirectory}
《filtering》true
《directory》src/test/resources
《directory》src/test/resources
《targetPath》${project.build.testOutputDirectory}
《filtering》true
《groupId》org.codehaus.mojo
《artifactId》appassembler-maven-plugin
《version》1.10
《repositoryLayout》flat
《repositoryName》lib
《configurationDirectory》conf
《copyConfigurationDirectory》true
《configurationSourceDirectory》src/main/resources/env/${env}
《includeConfigurationDirectoryInClasspath》true
《target》${project.build.directory}
《id》${project.name}
《mainClass》com.mkl.local.m.MeApplication
《platform》jsw
《wrapperLogFile》logs/${project.name}.log
《generator》jsw
《include》macosx-universal-64
《include》linux-x86-32
《include》linux-x86-64
《include》windows-x86-32
《include》windows-x86-64
《name》configuration.directory.in.classpath.first
《value》conf
《name》set.default.REPO_DIR
《value》lib
《name》wrapper.ping.timeout
《value》300
《name》wrapper.startup.timeout
《value》300
《extraArgument》-Denv=${env}
《extraArgument》-Dspring.profiles.active=${env}
《extraArgument》-server
《extraArgument》-Xms${jvmSettings.initialMemorySize}m
《extraArgument》-Xmx${jvmSettings.maxMemorySize}m
《extraArgument》-Djava.net.preferIPv4Stack=true
并且中间加符号"-" 占一位 预留10个长度给到脚本自动追加hostname 做全局唯一标识 --》
--》
《extraArgument》-XX:+UseG1GC
《extraArgument》-XX:MaxGCPauseMillis=400
《extraArgument》-XX:InitiatingHeapOccupancyPercent=60
《extraArgument》-XX:+PrintGC
《extraArgument》-XX:+PrintGCDetails
《extraArgument》-XX:+PrintGCTimeStamps
《extraArgument》-XX:+PrintGCApplicationConcurrentTime
《extraArgument》-XX:+PrintGCApplicationStoppedTime
《extraArgument》-Xloggc:./logs/gc.log
《extraArgument》-Djava.security.egd=file:/dev/./urandom
《id》generate-jsw-scripts
《phase》package
《goal》generate-daemons
《goal》create-repository
《groupId》org.apache.maven.plugins
《artifactId》maven-assembly-plugin
《version》2.6
《phase》package
《goal》single
《descriptor》assembly/bin.xml
《groupId》org.apache.maven.plugins
《artifactId》maven-deploy-plugin
《version》2.8.2
《skip》true
《groupId》org.apache.maven.plugins
《artifactId》maven-surefire-plugin
《version》2.22.2
《skipTests》true
《groupId》org.springframework.boot
《artifactId》spring-boot-maven-plugin
《/build》
spring的配置文件怎么写
标准的Spring配置文件编写:
《?xml version="1.0" encoding="UTF-8"?》
***隐藏网址***
***隐藏网址***
***隐藏网址***
***隐藏网址***
***隐藏网址***
***隐藏网址***
***隐藏网址***
default-autowire="byName" default-lazy-init="true"》
《!-- 配置数据源 --》
《bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"》
《property name="driverClassName"》
《value》com.mysql.jdbc.Driver《/value》
《/property》
《property name="url"》
《value》
jdbc:mysql://localhost/ssh?characterEncoding=utf-8
《/value》
《/property》
《property name="username"》
《value》root《/value》
《/property》
《property name="password"》
《value》123《/value》
《/property》
《/bean》
《!--配置SessionFactory --》
《bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"》
《property name="dataSource"》
《ref bean="dataSource" /》
《/property》
《property name="mappingResources"》
《list》
《value》com/ssh/pojo/User.hbm.xml《/value》
《/list》
《/property》
《property name="hibernateProperties"》
《props》
《prop key="hibernate.show_sql"》true《/prop》
《/props》
《/property》
《/bean》
《!-- 事务管理 --》
《bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager"》
《property name="sessionFactory"》
《ref bean="sessionFactory" /》
《/property》
《/bean》
《!-- hibernateTemplate --》
《bean id="hibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate"》
《property name="sessionFactory"》
《ref bean="sessionFactory" /》
《/property》
《/bean》
《!-- 配置数据持久层 --》
《bean id="userDao"
class="com.ssh.dao.impl.UserDaoImpl"》
《property name="hibernateTemplate" ref="hibernateTemplate"》《/property》
《/bean》
《!-- 配置业务逻辑层 --》
《bean id="userService"
class="com.ssh.service.impl.UserServiceImpl"》
《property name="userDao" ref="userDao"》《/property》
《/bean》
《!-- 配置控制层 --》
《bean id="UserAction"
class="com.ssh.action.UserAction" scope="prototype"》
《property name="userService" ref="userService"》《/property》
《/bean》
《!-- 配置pojo --》
《bean id="User" class="com.ssh.pojo.User" scope="prototype"/》
《/beans》
spring4零配置怎么配置默认html页面
spring4零配置配置默认html页面的方法是在视图解析器中配置实现的。
比如,项目中以打开login.html为例来说明:
以下是spring-servlet.xml的配置,其中重点是property中的html属性配置。
《?xml version="1.0" encoding="UTF-8"?》
《!--《context:annotation-config /》--》
《!-- 把标记了@Controller注解的类转换为bean --》
《context:component-scan base-package="controller" /》
《!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 --》
《bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /》
《!-- 设置freeMarker的配置文件路径 --》
《bean id="freemarkerConfiguration"
class="org.springframework.beans.factory.config.PropertiesFactoryBean"》
《!--注释掉的下方代码是指引freemarker的基本信息的配置位置,因为我已经将配置信息移到了applicationContext文件下,所以这里就没必要存在了,不注释也不会有问题的 --》
《!--《property name="location" value="classpath:/WEB-INF/config/freemarker.properties" /》--》
《/bean》
《bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"》
《property name="exposeRequestAttributes" value="true" /》
《property name="exposeSessionAttributes" value="true" /》
《property name="viewClass"》
《value》org.springframework.web.servlet.view.freemarker.FreeMarkerView《/value》
《/property》
《property name="cache"》《value》true《/value》《/property》
《!--这里需要注意一下,我注释了下面这样一行代码,这行代码的意思就是指引freemarker需要解析的文件的位置。注释掉原因是因为
applicationContext.xml里有这样一行代码:《property name="templateLoaderPath" value="/WEB-INF/views/" /》 已经指定了视图位置。如果我们这里依然保留下方代码,页面回报406的找不到的错误 --》
《!--《property name="prefix"》《value》/WEB-INF/views/《/value》《/property》--》
《property name="suffix"》《value》.html《/value》《/property》
《property name="contentType"》
《value》text/html; charset=UTF-8《/value》
《/property》
《/bean》
《/beans》
运行结果:

更多文章:
二郎腿吧我发表了一张图片贴(求一张脚踝扭伤的真实图片,连续工作两个星期了,答应了女朋友出去玩大家帮帮忙)
2026年9月10日 14:10
angular momentum公式(impulse和momentum的公式)
2026年9月10日 13:50
web service 技术发展(web service技术是不是落后了)
2026年9月10日 12:20
clouddata(iCloud怎么读 iCloud如何读)
2026年9月10日 08:30






