开始学习spring了,小白而已,希望大家来指出不足
spring基础包资源:https://download.csdn.net/download/m0_43444513/12821287
-
在实际的开发阶段,通常在服务器采用三层框架的形式:表现层(controller)、业务层(service)、数据持久层(dao)
-
初识spring
spring有俩大主要的功能:控制反转(Ioc) 和 面向切面编程(Aop)spring 把Java的对象、组件 作为JavaBean来进行管理,创建、配置。。。。
使用 spring ioc的过程
1)、引入相应的jar包
2)、编辑SpringConfig.xml
<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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> </beans>
3)编写log4j.properties
# For JBoss: Avoid to setup Log4J outside $JBOSS_HOME/server/default/deploy/log4j.xml! # For all other servers: Comment out the Log4J listener in web.xml to activate Log4J. log4j.rootLogger=INFO, stdout, logfile log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n log4j.appender.logfile=org.apache.log4j.RollingFileAppender log4j.appender.logfile.File=E:/springLog.log log4j.appender.logfile.MaxFileSize=512KB # Keep three backup files. log4j.appender.logfile.MaxBackupIndex=3 # Pattern to output: date priority [category] - message log4j.appender.logfile.layout=org.apache.log4j.PatternLayout log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
3)创建com.qiao.spring的包,写入Boy、Girl的类
在xml文件中添加
<bean id="aa" class="com.qiao.spring.Boy"> </bean> <bean id="girl" class="com.qiao.spring.Girl"> <!-- 维护依赖关系,使用依赖注入 name属性: 当前girl对象依赖的对象引用名 注入给boy的对象引用名 --> <property name="boy" ref="aa"></property> </bean>
4)在Test中创建ApplicationContext的对像,即spring的容器
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
5)通过getbean()来从容器中获取相应的对象
Girl girl = (Girl) ac.getBean(“girl”);
System.out.println(girl);