├── .gitignore
├── LICENSE
├── chapter1
├── pom.xml
└── src
│ └── main
│ └── java
│ └── com
│ └── test
│ └── chapter1
│ └── HelloWorldController.java
├── chapter10
├── pom.xml
└── src
│ └── main
│ ├── java
│ └── com
│ │ └── test
│ │ └── log
│ │ └── helloworld
│ │ └── HelloWorld.java
│ └── resources
│ └── log4j2.xml
├── chapter11
├── pom.xml
└── src
│ └── main
│ └── java
│ └── com
│ └── test
│ └── mvc
│ ├── controller
│ ├── FreeMarkerController.java
│ ├── HelloSpringMvcController.java
│ ├── InterceptorController.java
│ ├── JsonController.java
│ └── XmlController.java
│ ├── interceptor
│ └── HelloInterceptor.java
│ └── model
│ ├── Book.java
│ └── User.java
├── chapter12
├── pom.xml
└── src
│ └── main
│ ├── java
│ └── com
│ │ └── test
│ │ └── mybatis
│ │ ├── controller
│ │ └── CustomerController.java
│ │ ├── dao
│ │ └── CustomerDao.java
│ │ ├── entity
│ │ └── Customer.java
│ │ └── service
│ │ ├── CustomerService.java
│ │ └── CustomerServiceImpl.java
│ └── resources
│ ├── customer.sql
│ ├── jdbc.properties
│ ├── mybatis-customer-mapper.xml
│ └── springmybatis.xml
├── chapter13
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── com
│ │ │ └── test
│ │ │ └── transaction
│ │ │ ├── dao
│ │ │ └── AccountBalanceDao.java
│ │ │ └── entity
│ │ │ └── AccountBalance.java
│ └── resources
│ │ └── mybatis-accountbalance-mapper.xml
│ └── test
│ ├── java
│ └── com
│ │ └── test
│ │ └── transaction
│ │ └── AccountTransactionTest.java
│ └── resources
│ ├── account_balance.sql
│ ├── jdbc.properties
│ └── spring-transaction-mybatis.xml
├── chapter14
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── com
│ │ │ └── test
│ │ │ └── redis
│ │ │ └── demo
│ │ │ ├── dao
│ │ │ └── BookDao.java
│ │ │ ├── impl
│ │ │ └── BookServiceImpl.java
│ │ │ ├── model
│ │ │ └── Book.java
│ │ │ └── service
│ │ │ └── BookService.java
│ └── resources
│ │ ├── book.sql
│ │ └── mybatis-book-mapper.xml
│ └── test
│ ├── java
│ └── com
│ │ └── test
│ │ └── demo
│ │ └── BookServiceTest.java
│ └── resources
│ ├── mybatis_jdbc.properties
│ ├── redis.properties
│ ├── spring-book.xml
│ └── spring-redis.xml
├── chapter15
├── pom.xml
└── src
│ ├── main
│ └── java
│ │ └── com
│ │ └── test
│ │ └── zk
│ │ ├── demo
│ │ └── ZookeeperClient.java
│ │ ├── lock
│ │ ├── DistributeClient.java
│ │ └── SharedResource.java
│ │ └── pubsub
│ │ ├── NodeCacheSubscriber.java
│ │ ├── PathChildrenCacheSubscriber.java
│ │ ├── Publisher.java
│ │ └── TreeCacheSubscriber.java
│ └── test
│ ├── java
│ └── com
│ │ └── test
│ │ └── zk
│ │ ├── demo
│ │ ├── NodeCachePubSubTest.java
│ │ ├── PathChildrenCachePubSubTest.java
│ │ ├── TreeCachePubSubTest.java
│ │ └── ZookeeperClientTest.java
│ │ └── lock
│ │ └── DistributeLockTest.java
│ └── resources
│ └── spring-zookeeper.xml
├── chapter16
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── com
│ │ │ └── test
│ │ │ └── kafka
│ │ │ ├── consumer
│ │ │ └── MessageConsumer.java
│ │ │ └── producer
│ │ │ └── MessageProducer.java
│ └── resources
│ │ └── spring-kafka.xml
│ └── test
│ └── java
│ └── com
│ └── test
│ └── kafka
│ └── SpringKafkaTest.java
├── chapter17
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── com
│ │ │ └── test
│ │ │ └── mycat
│ │ │ ├── dao
│ │ │ ├── CustomerDao.java
│ │ │ ├── CustomerOrderDao.java
│ │ │ └── ItemDao.java
│ │ │ └── model
│ │ │ ├── Customer.java
│ │ │ ├── CustomerOrder.java
│ │ │ └── Item.java
│ └── resources
│ │ ├── jdbc.properties
│ │ ├── mapper
│ │ ├── mybatis-customer-mapper.xml
│ │ ├── mybatis-customer_order-mapper.xml
│ │ └── mybatis-item-mapper.xml
│ │ └── spring-mycat.xml
│ └── test
│ └── java
│ └── com
│ └── test
│ └── mycat
│ ├── CustomerDaoTest.java
│ ├── CustomerOrderDaoTest.java
│ └── ItemDaoTest.java
├── chapter18
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── com
│ │ │ └── test
│ │ │ └── sharding
│ │ │ ├── config
│ │ │ ├── DataSourceConfig.java
│ │ │ ├── DemoDatabaseShardingAlgorithm.java
│ │ │ └── DemoTableShardingAlgorithm.java
│ │ │ ├── dao
│ │ │ └── ShopInfoDao.java
│ │ │ ├── entity
│ │ │ └── ShopInfo.java
│ │ │ └── service
│ │ │ └── ShopService.java
│ └── resources
│ │ ├── jdbc.properties
│ │ ├── mapper
│ │ └── ShopInfoMapper.xml
│ │ ├── spring-sharding.xml
│ │ └── spring-sharing.sql
│ └── test
│ └── java
│ └── com
│ └── test
│ └── sharding
│ └── ShopInfoTest.java
├── chapter19
├── api
│ ├── pom.xml
│ └── src
│ │ └── main
│ │ └── java
│ │ └── com
│ │ └── test
│ │ └── dubbo
│ │ └── api
│ │ └── HelloService.java
├── consumer
│ ├── pom.xml
│ └── src
│ │ ├── main
│ │ └── resources
│ │ │ └── dubbo-consumer.xml
│ │ └── test
│ │ └── java
│ │ └── com
│ │ └── test
│ │ └── dubbo
│ │ └── consumer
│ │ └── DubboConsumerTest.java
├── pom.xml
├── provider
│ ├── pom.xml
│ └── src
│ │ ├── main
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── test
│ │ │ │ └── dubbo
│ │ │ │ └── provider
│ │ │ │ └── HelloServiceImpl.java
│ │ └── resources
│ │ │ └── dubbo-provider.xml
│ │ └── test
│ │ └── java
│ │ └── com
│ │ └── test
│ │ └── dubbo
│ │ └── provider
│ │ └── DubboProviderTest.java
└── spi
│ ├── pom.xml
│ └── src
│ └── main
│ ├── java
│ └── com
│ │ └── test
│ │ ├── ChineseSpiHelloServiceImpl.java
│ │ ├── EnglishSpiHelloServiceImpl.java
│ │ ├── SpiDemo.java
│ │ └── SpiHelloService.java
│ └── resources
│ └── META-INF
│ └── services
│ └── com.test.SpiHelloService
├── chapter2
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── com
│ │ │ └── test
│ │ │ ├── ioc
│ │ │ ├── BeanFactory创建流程图.png
│ │ │ ├── annotation
│ │ │ │ ├── HomeWork.java
│ │ │ │ ├── Student.java
│ │ │ │ └── readme.txt
│ │ │ ├── readme.txt
│ │ │ └── xml
│ │ │ │ ├── Deliverable.java
│ │ │ │ ├── Order.java
│ │ │ │ ├── Speakable.java
│ │ │ │ ├── User.java
│ │ │ │ └── readme.txt
│ │ │ ├── lifecycle
│ │ │ ├── beanlifcycle
│ │ │ │ ├── BeanLifecycle.java
│ │ │ │ └── readme.txt
│ │ │ ├── containerlifecycle
│ │ │ │ ├── ContainerLifecycle.java
│ │ │ │ └── readme.txt
│ │ │ └── factorylifecycle
│ │ │ │ ├── FactoryLifecycle.java
│ │ │ │ └── readme.txt
│ │ │ └── sourcecodelearning
│ │ │ ├── BookService.java
│ │ │ ├── BookServiceImpl.java
│ │ │ ├── PressService.java
│ │ │ ├── PressServiceImpl.java
│ │ │ ├── SourceCodeLearning.java
│ │ │ └── readme.txt
│ └── resources
│ │ ├── spring-chapter2-beanlifecycle.xml
│ │ ├── spring-chapter2-containerlifecycle.xml
│ │ ├── spring-chapter2-factorybeanlifecycle.xml
│ │ └── spring-chapter2.xml
│ └── test
│ └── java
│ ├── ioc
│ ├── annotation
│ │ ├── AnnotationTest.java
│ │ └── readme.txt
│ └── xml
│ │ ├── XmlTest.java
│ │ └── readme.txt
│ └── lifecycle
│ ├── beanlifcycle
│ └── BeanLifecycleTest.java
│ ├── containerlifecycle
│ └── ContainerLifecycleTest.java
│ └── factorylifecycle
│ └── FactoryLifecycleTest.java
├── chapter20
├── pom.xml
└── src
│ └── main
│ └── java
│ └── com
│ └── test
│ ├── abstractfactory
│ ├── AbstractFactory.java
│ ├── AbstractFactoryDemo.java
│ ├── ComputerFactory.java
│ ├── Dollar.java
│ ├── FactoryProducer.java
│ ├── Pound.java
│ ├── Price.java
│ ├── PriceFactory.java
│ └── RMB.java
│ ├── adapter
│ ├── AdapterPatternDemo.java
│ ├── AdvancedPlayer.java
│ ├── AudioPlay.java
│ ├── Mp4Play.java
│ ├── PlayAdapter.java
│ ├── Player.java
│ └── RmvbPlay.java
│ ├── bridge
│ ├── Bridge.java
│ ├── BridgePatternDemo.java
│ └── ComputerBridge.java
│ ├── builder
│ ├── AMD.java
│ ├── BuilderPattenDemo.java
│ ├── CPU.java
│ ├── Computer.java
│ ├── ComputerBuilder.java
│ ├── DELL.java
│ ├── Intel.java
│ ├── Item.java
│ ├── PHILIPS.java
│ ├── Packing.java
│ ├── Screen.java
│ └── Wrapper.java
│ ├── chain
│ ├── AbstractFilter.java
│ ├── ChainPatternDemo.java
│ ├── DirtyFilter.java
│ ├── PrivacyFilter.java
│ └── SensitiveFilter.java
│ ├── command
│ ├── AttentionCommand.java
│ ├── Broker.java
│ ├── Command.java
│ ├── CommandPatternDemo.java
│ ├── Soldier.java
│ └── StandEaseCommand.java
│ ├── common
│ ├── ASUS.java
│ ├── Computer.java
│ ├── Lenovo.java
│ └── MacBook.java
│ ├── composite
│ ├── CompositePatternDemo.java
│ └── Staff.java
│ ├── criteria
│ ├── AgeCriteria.java
│ ├── Criteria.java
│ ├── CriteriaPatternDemo.java
│ ├── MaleCriteria.java
│ └── Person.java
│ ├── decorator
│ ├── ComputerDecorator.java
│ ├── DecoratorPatternDemo.java
│ ├── GoldenComputerDecorator.java
│ ├── RedComputerDecorator.java
│ └── WhiteComputerDecorator.java
│ ├── facade
│ ├── ComputerFacade.java
│ └── FacadePatternDemo.java
│ ├── factorypattern
│ ├── ComputerFactory.java
│ └── ComputerFactoryDemo.java
│ ├── flyweight
│ ├── Computer.java
│ ├── ComputerFactory.java
│ ├── ComputerProducer.java
│ └── FlyweightPatternDemo.java
│ ├── interceptingfilter
│ ├── Client.java
│ ├── DirtyFilter.java
│ ├── Filter.java
│ ├── FilterChain.java
│ ├── FilterManager.java
│ ├── InterceptingFilterDemo.java
│ ├── PrivateFilter.java
│ ├── SensitiveFilter.java
│ └── Target.java
│ ├── interpreter
│ ├── AndExpression.java
│ ├── Expression.java
│ ├── InterpreterPatternDemo.java
│ ├── NumericalExpression.java
│ └── OrExpression.java
│ ├── iterator
│ ├── Container.java
│ ├── IntegerIterator.java
│ ├── IntegerRepository.java
│ ├── Iterator.java
│ └── IteratorPatternDemo.java
│ ├── mediator
│ ├── BBS.java
│ ├── MediatorPatternDemo.java
│ └── User.java
│ ├── memento
│ ├── Memento.java
│ ├── MementoPatternDemo.java
│ ├── Original.java
│ └── StateKeeper.java
│ ├── nullobject
│ ├── AbstractObject.java
│ ├── NullObject.java
│ ├── NullPatternDemo.java
│ ├── ObjectFactory.java
│ └── RealObject.java
│ ├── observer
│ ├── Observer.java
│ ├── ObserverPatternDemo.java
│ ├── WeChatPulic.java
│ └── WeChatUser.java
│ ├── prototype
│ ├── ASUS.java
│ ├── Computer.java
│ ├── ComputerPrototype.java
│ ├── Lenovo.java
│ ├── MacBook.java
│ └── PrototypePatternDemo.java
│ ├── proxy
│ ├── BuyHouse.java
│ ├── Customer.java
│ ├── CustomerProxy.java
│ └── ProxyPatternDemo.java
│ ├── singleton
│ ├── SingletonObject.java
│ └── SingletonObjectDemo.java
│ ├── state
│ ├── Rain.java
│ ├── State.java
│ ├── StatePatternDemo.java
│ ├── Sunshine.java
│ └── Weather.java
│ ├── strategy
│ ├── Addition.java
│ ├── Context.java
│ ├── Multiplication.java
│ ├── Strategy.java
│ ├── StrategyPatternDemo.java
│ └── Subtraction.java
│ └── template
│ ├── Game.java
│ ├── SuperMarie.java
│ ├── TemplatePatternDemo.java
│ └── Tetris.java
├── chapter3
├── pom.xml
└── src
│ └── main
│ ├── java
│ └── com
│ │ └── test
│ │ ├── aop
│ │ ├── cglib
│ │ │ ├── annotation
│ │ │ │ ├── CgLibAnnotationDemo.java
│ │ │ │ ├── CglibAnnotationHandler.java
│ │ │ │ ├── Desk.java
│ │ │ │ └── Table.java
│ │ │ └── xml
│ │ │ │ ├── CgLibXmlDemo.java
│ │ │ │ ├── CglibXmlAspect.java
│ │ │ │ ├── Desk.java
│ │ │ │ └── Table.java
│ │ └── jdk
│ │ │ ├── annotation
│ │ │ ├── FruitAnnotationHandler.java
│ │ │ ├── SpringAopAnnotationDemo.java
│ │ │ └── readme.txt
│ │ │ └── xml
│ │ │ ├── Apple.java
│ │ │ ├── Banana.java
│ │ │ ├── Fruit.java
│ │ │ ├── FruitHandler.java
│ │ │ ├── SpringAopXmlDemo.java
│ │ │ └── readme.txt
│ │ ├── aspectj
│ │ ├── advicetype
│ │ │ ├── AllAspect.java
│ │ │ ├── AllAspectDemo.java
│ │ │ ├── Person.java
│ │ │ └── readme.txt
│ │ ├── expression
│ │ │ ├── Factory.java
│ │ │ ├── FoodFactory.java
│ │ │ ├── PhoneFactory.java
│ │ │ ├── annotation
│ │ │ │ ├── AnnotationAspect.java
│ │ │ │ ├── AspectJExpressionDemo.java
│ │ │ │ ├── Log.java
│ │ │ │ └── readme.txt
│ │ │ ├── args
│ │ │ │ ├── ArgsAspect.java
│ │ │ │ ├── AspectJExpressionDemo.java
│ │ │ │ ├── FreshFoodFactory.java
│ │ │ │ ├── FrozenFoodFactory.java
│ │ │ │ ├── Listen.java
│ │ │ │ └── readme.txt
│ │ │ ├── execution
│ │ │ │ ├── AspectJExpressionDemo.java
│ │ │ │ ├── ExecutionAspect.java
│ │ │ │ └── readme.txt
│ │ │ ├── readme.txt
│ │ │ ├── thisexpression
│ │ │ │ ├── DefaultListener.java
│ │ │ │ ├── Listener.java
│ │ │ │ ├── ListenerAspect.java
│ │ │ │ ├── ThisAspect.java
│ │ │ │ ├── ThisExpressionDemo.java
│ │ │ │ └── readme.txt
│ │ │ └── within
│ │ │ │ ├── IPhoneFactory.java
│ │ │ │ ├── MobilePhoneFactory.java
│ │ │ │ ├── Monitor.java
│ │ │ │ ├── WithinAspect.java
│ │ │ │ ├── WithinExpressionDemo.java
│ │ │ │ └── readme.txt
│ │ └── readme.txt
│ │ ├── cglibdynamicproxy
│ │ ├── Cat.java
│ │ ├── CatMethodInterceptor.java
│ │ ├── CglibDynamicProxyDemo.java
│ │ └── readme.txt
│ │ ├── jdkdynamicproxy
│ │ ├── Animal.java
│ │ ├── AnimalInvocationHandler.java
│ │ ├── Dog.java
│ │ ├── JDKDynamicProxyDemo.java
│ │ └── readme.txt
│ │ ├── sourcecodelearning
│ │ ├── AopSourceCodeLearningDemo.java
│ │ ├── Log.java
│ │ ├── LogAroundAdvice.java
│ │ └── Target.java
│ │ └── springadvicetype
│ │ ├── Waiter.java
│ │ ├── afterretuningadvice
│ │ ├── SpringAfterReturningAdvice.java
│ │ ├── SpringAfterReturningAdviceDemo.java
│ │ └── readme.txt
│ │ ├── beforeadvice
│ │ ├── SpringBeforeAdvice.java
│ │ ├── SpringBeforeAdviceDemo.java
│ │ └── readme.txt
│ │ ├── introductioninterceptor
│ │ ├── Management.java
│ │ ├── Manager.java
│ │ ├── SpringIntroductionInterceptorDemo.java
│ │ └── readme.txt
│ │ ├── methodinterceptor
│ │ ├── SpringMethodInterceptor.java
│ │ ├── SpringMethodInterceptorDemo.java
│ │ └── readme.txt
│ │ └── throwsadvice
│ │ ├── SpringThrowsAdvice.java
│ │ ├── SpringThrowsAdviceDemo.java
│ │ └── readme.txt
│ └── resources
│ ├── spring-chapter3-annotationaop.xml
│ ├── spring-chapter3-annotationcglibaop.xml
│ ├── spring-chapter3-aoptype.xml
│ ├── spring-chapter3-aspectjannotationexpression.xml
│ ├── spring-chapter3-aspectjargsexpression.xml
│ ├── spring-chapter3-aspectjexecutionexpression.xml
│ ├── spring-chapter3-aspectjtargetexpression.xml
│ ├── spring-chapter3-aspectjthisexpression.xml
│ ├── spring-chapter3-aspectjwithinexpression.xml
│ ├── spring-chapter3-sourcecodelearning.xml
│ ├── spring-chapter3-springaoptype.xml
│ ├── spring-chapter3-springintroductioninterceptor.xml
│ ├── spring-chapter3-xmlaop.xml
│ └── spring-chapter3-xmlcglib.xml
├── chapter5
├── pom.xml
└── src
│ └── main
│ └── java
│ └── com
│ └── test
│ └── jdk8
│ ├── defaultmethod
│ ├── Bus.java
│ ├── Car.java
│ ├── DefaultMethodDemo.java
│ ├── Vehicle.java
│ └── Whistle.java
│ └── lambda
│ ├── Java8LambdaDemo.java
│ ├── Java8LambdaInCollectionDemo.java
│ ├── Java8LambdaInStreamDemo.java
│ ├── Java8LambdaInThreadDemo.java
│ ├── Java8LambdaScopeDemo.java
│ └── Person.java
├── chapter6-webflux
├── .gitignore
├── .mvn
│ └── wrapper
│ │ ├── maven-wrapper.jar
│ │ └── maven-wrapper.properties
├── mvnw
├── mvnw.cmd
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── com
│ │ │ └── test
│ │ │ └── chapter6webflux
│ │ │ ├── Chapter6WebfluxApplication.java
│ │ │ ├── funtctional
│ │ │ ├── handler
│ │ │ │ └── UserHandler.java
│ │ │ └── router
│ │ │ │ └── RoutingConfiguration.java
│ │ │ ├── model
│ │ │ └── User.java
│ │ │ └── service
│ │ │ ├── UserService.java
│ │ │ └── impl
│ │ │ └── UserServiceImpl.java
│ └── resources
│ │ └── application.properties
│ └── test
│ └── java
│ └── com
│ └── test
│ └── chapter6webflux
│ └── Chapter6WebfluxApplicationTests.java
├── chapter6
├── pom.xml
└── src
│ └── main
│ └── java
│ └── com
│ └── test
│ ├── reactor
│ ├── FilterOperationDemo.java
│ ├── FirstReactorDemo.java
│ ├── FlatMapOperationDemo.java
│ ├── MapOperatorDemo.java
│ ├── SchedulerOperationDemo.java
│ ├── StepVerifierDemo.java
│ └── ZipOperationDemo.java
│ ├── traditionalprogram
│ └── Test.java
│ └── webflux
│ └── HelloController.java
├── chapter7
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── com
│ │ │ └── test
│ │ │ ├── resttemplate
│ │ │ ├── DateController.java
│ │ │ ├── DateService.java
│ │ │ └── DateServiceImpl.java
│ │ │ └── webclient
│ │ │ ├── DateWebFluxController.java
│ │ │ ├── DateWebFluxService.java
│ │ │ └── DateWebFluxServiceImpl.java
│ └── resources
│ │ └── chapter7.xml
│ └── test
│ └── java
│ └── com
│ └── test
│ ├── resttemplate
│ └── RestTemplateTest.java
│ └── webclient
│ └── WebClientTest.java
├── chapter8
├── pom.xml
└── src
│ ├── main
│ └── kotlin
│ │ └── com
│ │ └── test
│ │ └── spring5
│ │ ├── Hello.kt
│ │ └── kotlindemo
│ │ ├── FunctionDemo.kt
│ │ ├── ParamDefinitionDemo.kt
│ │ ├── clazz
│ │ ├── KotlinAndJava.kt
│ │ └── User.kt
│ │ └── web
│ │ ├── SpringBootWithKotlinApplication.kt
│ │ ├── Staff.kt
│ │ ├── StaffController.kt
│ │ ├── StaffService.kt
│ │ └── StaffServiceImpl.kt
│ └── test
│ └── kotlin
│ └── com
│ └── test
│ └── spring5
│ └── HelloTest.kt
├── chapter9
├── pom.xml
└── src
│ ├── main
│ └── java
│ │ └── com
│ │ └── test
│ │ └── demo
│ │ └── Calculator.java
│ └── test
│ └── java
│ └── com
│ └── test
│ └── demo
│ └── CalculatorTests.java
├── pom.xml
└── spring5all
├── pom.xml
└── src
└── main
├── java
└── com
│ └── test
│ └── Test.java
├── resources
├── applicationContext.xml
└── springmvc.xml
└── webapp
├── WEB-INF
└── web.xml
├── index.jsp
├── pages
├── hello.jsp
└── hellospringmvc.jsp
└── template
└── helloFreeMarker.ftl
/.gitignore:
--------------------------------------------------------------------------------
1 | .DS_Store
2 | .idea/
3 | out/
4 | spring5projectdemo.iml
5 | target/
6 |
7 | chapter1/chapter1.iml
8 | chapter2/chapter2.iml
9 | project.demo.iml
10 | spring5/spring5.iml
11 | *.iml
12 | log/
--------------------------------------------------------------------------------
/chapter1/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | project.demo
7 | com.test.spring5
8 | 1.0-SNAPSHOT
9 |
10 | 4.0.0
11 | chapter1
12 | ${spring5.chapter1.version}
13 | jar
14 |
15 |
--------------------------------------------------------------------------------
/chapter1/src/main/java/com/test/chapter1/HelloWorldController.java:
--------------------------------------------------------------------------------
1 | package com.test.chapter1;
2 |
3 | import org.springframework.web.bind.annotation.RequestMapping;
4 | import org.springframework.web.bind.annotation.RestController;
5 |
6 | @RestController
7 | @RequestMapping("/")
8 | public class HelloWorldController {
9 | @RequestMapping("hello")
10 | public String sayHello() {
11 | return "Hello World";
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/chapter10/src/main/java/com/test/log/helloworld/HelloWorld.java:
--------------------------------------------------------------------------------
1 | package com.test.log.helloworld;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 |
6 | /**
7 | * @Author zhouguanya
8 | * @Date 2018/12/11
9 | * @Description 使用log4j2实现的第一个HelloWord程序
10 | */
11 | public class HelloWorld {
12 | private static final Logger LOGGER = LoggerFactory.getLogger(HelloWorld.class);
13 | public static void main(String[] args) {
14 | LOGGER.info("hello world");
15 | LOGGER.warn("hello world");
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/chapter11/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | project.demo
7 | com.test.spring5
8 | 1.0-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | com.test.spring5
13 | chapter11
14 |
15 |
16 |
17 | org.freemarker
18 | freemarker
19 | 2.3.28
20 |
21 |
22 | com.fasterxml.jackson.core
23 | jackson-databind
24 | 2.9.7
25 |
26 |
27 |
--------------------------------------------------------------------------------
/chapter11/src/main/java/com/test/mvc/controller/FreeMarkerController.java:
--------------------------------------------------------------------------------
1 | package com.test.mvc.controller;
2 |
3 | import org.springframework.stereotype.Controller;
4 | import org.springframework.ui.ModelMap;
5 | import org.springframework.web.bind.annotation.RequestMapping;
6 |
7 | /**
8 | * @Author zhouguanya
9 | * @Date 2018/12/16
10 | * @Description
11 | */
12 | @Controller
13 | public class FreeMarkerController {
14 | @RequestMapping("/hello/freemarker")
15 | public String hello(ModelMap map){
16 | map.put("msg", "Welcome to FreeMarker World");
17 | return "helloFreeMarker";
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/chapter11/src/main/java/com/test/mvc/controller/HelloSpringMvcController.java:
--------------------------------------------------------------------------------
1 | package com.test.mvc.controller;
2 |
3 | import org.springframework.stereotype.Controller;
4 | import org.springframework.ui.ModelMap;
5 | import org.springframework.web.bind.annotation.RequestMapping;
6 | import org.springframework.web.bind.annotation.RequestMethod;
7 |
8 | /**
9 | * @Author zhouguanya
10 | * @Date 2018/12/15
11 | * @Description
12 | */
13 | @Controller
14 | @RequestMapping("/hello/springmvc")
15 | public class HelloSpringMvcController {
16 |
17 | @RequestMapping(method = RequestMethod.GET)
18 | public String printHello(ModelMap model) {
19 | model.addAttribute("message", "Welcome to Spring MVC");
20 | return "hellospringmvc";
21 | }
22 |
23 | }
24 |
--------------------------------------------------------------------------------
/chapter11/src/main/java/com/test/mvc/controller/InterceptorController.java:
--------------------------------------------------------------------------------
1 | package com.test.mvc.controller;
2 |
3 | import org.springframework.stereotype.Controller;
4 | import org.springframework.web.bind.annotation.RequestMapping;
5 | import org.springframework.web.bind.annotation.ResponseBody;
6 |
7 | /**
8 | * @Author zhouguanya
9 | * @Date 2018/12/17
10 | * @Description SpringMVC拦截器使用
11 | */
12 | @Controller
13 | @RequestMapping("/interceptor")
14 | public class InterceptorController {
15 |
16 | @RequestMapping("/hello")
17 | @ResponseBody
18 | public String hello() {
19 | return "hello interceptor";
20 | }
21 |
22 | @RequestMapping("/bye")
23 | @ResponseBody
24 | public String bye() {
25 | return "bye bye interceptor";
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/chapter11/src/main/java/com/test/mvc/controller/JsonController.java:
--------------------------------------------------------------------------------
1 | package com.test.mvc.controller;
2 |
3 | import com.test.mvc.model.Book;
4 | import org.springframework.stereotype.Controller;
5 | import org.springframework.web.bind.annotation.RequestMapping;
6 | import org.springframework.web.bind.annotation.ResponseBody;
7 |
8 | /**
9 | * @Author zhouguanya
10 | * @Date 2018/12/16
11 | * @Description
12 | */
13 | @Controller
14 | public class JsonController {
15 | @RequestMapping("/hello/book")
16 | @ResponseBody
17 | public Book getBook() {
18 | Book book = new Book();
19 | book.setName("Spring 5");
20 | book.setPrice(50);
21 | book.setAuthor("Michael");
22 | return book;
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/chapter11/src/main/java/com/test/mvc/controller/XmlController.java:
--------------------------------------------------------------------------------
1 | package com.test.mvc.controller;
2 |
3 | import com.test.mvc.model.User;
4 | import org.springframework.stereotype.Controller;
5 | import org.springframework.web.bind.annotation.RequestMapping;
6 | import org.springframework.web.bind.annotation.ResponseBody;
7 |
8 | /**
9 | * @Author zhouguanya
10 | * @Date 2018/12/16
11 | * @Description
12 | */
13 | @Controller
14 | public class XmlController {
15 | @RequestMapping("/hello/xml")
16 | @ResponseBody
17 | public User getUser() {
18 | User user = new User();
19 | user.setUserName("Michael");
20 | user.setUserAge(20);
21 | return user;
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/chapter11/src/main/java/com/test/mvc/model/Book.java:
--------------------------------------------------------------------------------
1 | package com.test.mvc.model;
2 |
3 | /**
4 | * @Author zhouguanya
5 | * @Date 2018/12/16
6 | * @Description
7 | */
8 | public class Book {
9 | private String name;
10 | private int price;
11 | private String author;
12 | public String getName() {
13 | return name;
14 | }
15 |
16 | public void setName(String name) {
17 | this.name = name;
18 | }
19 |
20 | public int getPrice() {
21 | return price;
22 | }
23 |
24 | public void setPrice(int price) {
25 | this.price = price;
26 | }
27 |
28 | public String getAuthor() {
29 | return author;
30 | }
31 |
32 | public void setAuthor(String author) {
33 | this.author = author;
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/chapter11/src/main/java/com/test/mvc/model/User.java:
--------------------------------------------------------------------------------
1 | package com.test.mvc.model;
2 |
3 | import javax.xml.bind.annotation.XmlElement;
4 | import javax.xml.bind.annotation.XmlRootElement;
5 | import java.io.Serializable;
6 |
7 | /**
8 | * @Author zhouguanya
9 | * @Date 2018/12/16
10 | * @Description
11 | */
12 | @XmlRootElement(name = "user")
13 | public class User implements Serializable {
14 | private String userName;
15 | private int userAge;
16 |
17 | public String getUserName() {
18 | return userName;
19 | }
20 | @XmlElement
21 | public void setUserName(String userName) {
22 | this.userName = userName;
23 | }
24 |
25 | public int getUserAge() {
26 | return userAge;
27 | }
28 | @XmlElement
29 | public void setUserAge(int userAge) {
30 | this.userAge = userAge;
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/chapter12/src/main/java/com/test/mybatis/dao/CustomerDao.java:
--------------------------------------------------------------------------------
1 | package com.test.mybatis.dao;
2 |
3 | import com.test.mybatis.entity.Customer;
4 |
5 | /**
6 | * @Author: zhouguanya
7 | * @Date: 2018/12/24
8 | * @Description: 数据库访问层
9 | */
10 | public interface CustomerDao {
11 | /**
12 | * 保存客户信息
13 | */
14 | int save(Customer customer);
15 |
16 | /**
17 | * 更新用户信息
18 | */
19 | int update(Customer customer);
20 |
21 | /**
22 | * 查询用户信息
23 | */
24 | Customer query(int id);
25 |
26 | /**
27 | * 删除用户信息
28 | */
29 | int delete(int id);
30 | }
31 |
--------------------------------------------------------------------------------
/chapter12/src/main/java/com/test/mybatis/service/CustomerService.java:
--------------------------------------------------------------------------------
1 | package com.test.mybatis.service;
2 |
3 | import com.test.mybatis.entity.Customer;
4 |
5 | /**
6 | * @Author: zhouguanya
7 | * @Date: 2018/12/24
8 | * @Description: service接口
9 | */
10 | public interface CustomerService {
11 | /**
12 | * 保存客户信息
13 | */
14 | int save(Customer customer);
15 |
16 | /**
17 | * 更新用户信息
18 | */
19 | int update(Customer customer);
20 |
21 | /**
22 | * 查询用户信息
23 | */
24 | Customer query(int customerId);
25 |
26 | /**
27 | * 删除用户信息
28 | */
29 | int delete(int customerId);
30 | }
31 |
--------------------------------------------------------------------------------
/chapter12/src/main/resources/customer.sql:
--------------------------------------------------------------------------------
1 | CREATE TABLE `customer` (
2 | `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '客户id',
3 | `name` varchar(20) DEFAULT '' COMMENT '客户姓名',
4 | `phone` varchar(11) DEFAULT '' COMMENT '客户手机号',
5 | `adddate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '添加时间',
6 | `updatedate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
7 | PRIMARY KEY (`id`)
8 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8
--------------------------------------------------------------------------------
/chapter12/src/main/resources/jdbc.properties:
--------------------------------------------------------------------------------
1 | driver=com.mysql.jdbc.Driver
2 | #jdbc连接
3 | url=jdbc:mysql://127.0.0.1:3306/test
4 | #MySQL用户名
5 | username=root
6 | #MySQL密码
7 | password=123456
8 |
9 | idleConnectionTestPeriod=60
10 | maxIdleTime=240
11 | acquireIncrement=5
12 | initialPoolSize=10
13 | maxPoolSize=30
14 | minPoolSize=10
15 | autoCommitOnClose=false
16 | checkoutTimeout=1000
17 | acquireRetryAttempts=2
--------------------------------------------------------------------------------
/chapter13/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | project.demo
7 | com.test.spring5
8 | 1.0-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | chapter13
13 |
--------------------------------------------------------------------------------
/chapter13/src/main/java/com/test/transaction/dao/AccountBalanceDao.java:
--------------------------------------------------------------------------------
1 | package com.test.transaction.dao;
2 |
3 | import com.test.transaction.entity.AccountBalance;
4 |
5 | /**
6 | * @Author: zhouguanya
7 | * @Date: 2018/12/27
8 | * @Description: MyBatis会通过动态代理注入
9 | */
10 | public interface AccountBalanceDao {
11 | /**
12 | * 查询账户余额
13 | */
14 | int queryAccountByCustomerId(int id);
15 |
16 | /**
17 | * 保存账户余额
18 | */
19 | int saveAccountBalance(AccountBalance accountBalance);
20 |
21 | /**
22 | * 更新账户余额
23 | */
24 | int updateAccountBalance(AccountBalance accountBalance);
25 | }
26 |
--------------------------------------------------------------------------------
/chapter13/src/test/resources/account_balance.sql:
--------------------------------------------------------------------------------
1 | CREATE TABLE `account_balance` (
2 | `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键',
3 | `customerId` int(11) NOT NULL COMMENT '客户号',
4 | `balance` decimal(10,0) DEFAULT NULL COMMENT '账户余额',
5 | `adddate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '添加时间',
6 | `updatedate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
7 | PRIMARY KEY (`id`),
8 | KEY (`customerId`)
9 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8
--------------------------------------------------------------------------------
/chapter13/src/test/resources/jdbc.properties:
--------------------------------------------------------------------------------
1 | driver=com.mysql.jdbc.Driver
2 | #jdbc连接
3 | url=jdbc:mysql://127.0.0.1:3306/test
4 | #MySQL用户名
5 | username=root
6 | #MySQL密码
7 | password=123456
8 |
9 | idleConnectionTestPeriod=60
10 | maxIdleTime=240
11 | acquireIncrement=5
12 | initialPoolSize=10
13 | maxPoolSize=30
14 | minPoolSize=10
15 | autoCommitOnClose=false
16 | checkoutTimeout=1000
17 | acquireRetryAttempts=2
--------------------------------------------------------------------------------
/chapter14/src/main/java/com/test/redis/demo/dao/BookDao.java:
--------------------------------------------------------------------------------
1 | package com.test.redis.demo.dao;
2 |
3 | import com.test.redis.demo.model.Book;
4 |
5 | /**
6 | * @Author: zhouguanya
7 | * @Date: 2019/01/05
8 | * @Description:
9 | */
10 | public interface BookDao {
11 | int save(Book book);
12 | Book query(int id);
13 | }
14 |
--------------------------------------------------------------------------------
/chapter14/src/main/java/com/test/redis/demo/service/BookService.java:
--------------------------------------------------------------------------------
1 | package com.test.redis.demo.service;
2 |
3 |
4 | import com.test.redis.demo.model.Book;
5 |
6 | /**
7 | * @Author: zhouguanya
8 | * @Date: 2019/01/05
9 | * @Description:
10 | */
11 | public interface BookService {
12 | int save(Book book);
13 | Book query(int id);
14 | }
15 |
--------------------------------------------------------------------------------
/chapter14/src/main/resources/book.sql:
--------------------------------------------------------------------------------
1 | CREATE TABLE `book` (
2 | `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键',
3 | `name` varchar(50) NOT NULL COMMENT '书名',
4 | `price` int(11) DEFAULT NULL COMMENT '价格',
5 | `adddate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '添加时间',
6 | `updatedate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
7 | PRIMARY KEY (`id`),
8 | KEY (`id`)
9 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8
--------------------------------------------------------------------------------
/chapter14/src/test/resources/mybatis_jdbc.properties:
--------------------------------------------------------------------------------
1 | mysql.driver=com.mysql.jdbc.Driver
2 | #jdbc连接
3 | mysql.url=jdbc:mysql://127.0.0.1:3306/test
4 | #MySQL用户名
5 | mysql.username=root
6 | #MySQL密码
7 | mysql.password=123456
--------------------------------------------------------------------------------
/chapter14/src/test/resources/redis.properties:
--------------------------------------------------------------------------------
1 | #属性文件
2 | redis.host1=127.0.0.1
3 | redis.port1=6001
4 | redis.host2=127.0.0.1
5 | redis.port2=6002
6 | redis.host3=127.0.0.1
7 | redis.port3=6003
8 | redis.host4=127.0.0.1
9 | redis.port4=6004
10 | redis.host5=127.0.0.1
11 | redis.port5=6005
12 | redis.host6=127.0.0.1
13 | redis.port6=6006
14 | redis.host7=127.0.0.1
15 | redis.port7=6007
16 | redis.host8=127.0.0.1
17 | redis.port8=6008
18 | redis.maxIdle=50
19 | redis.maxActive=100
20 | redis.maxWait=5000
21 | redis.testOnBorrow=true
--------------------------------------------------------------------------------
/chapter15/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | project.demo
7 | com.test.spring5
8 | 1.0-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | chapter15
13 |
14 |
15 |
16 | org.apache.curator
17 | curator-framework
18 | 2.11.1
19 |
20 |
21 | org.apache.curator
22 | curator-recipes
23 | 2.11.1
24 |
25 |
26 |
--------------------------------------------------------------------------------
/chapter15/src/main/java/com/test/zk/lock/SharedResource.java:
--------------------------------------------------------------------------------
1 | package com.test.zk.lock;
2 |
3 | import java.util.concurrent.atomic.AtomicBoolean;
4 |
5 | /**
6 | * @Author: zhouguanya
7 | * @Date: 2019/01/09
8 | * @Description: 模拟一个共享资源,只能单线程访问
9 | */
10 | public class SharedResource {
11 | private final AtomicBoolean shareResourceInUse = new AtomicBoolean(false);
12 |
13 | public void use() throws InterruptedException {
14 | // 真实环境中我们会在这里访问/维护一个共享的资源
15 | if (!shareResourceInUse.compareAndSet(false, true)) {
16 | throw new IllegalStateException("Needs to be used by one client at a time");
17 | }
18 | try {
19 | System.out.println("共享资源操作中");
20 | Thread.sleep(1);
21 | } finally {
22 | shareResourceInUse.set(false);
23 | }
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/chapter16/src/main/java/com/test/kafka/consumer/MessageConsumer.java:
--------------------------------------------------------------------------------
1 | package com.test.kafka.consumer;
2 |
3 | import org.apache.kafka.clients.consumer.ConsumerRecord;
4 | import org.springframework.kafka.listener.MessageListener;
5 | import org.springframework.stereotype.Component;
6 |
7 | /**
8 | * @Author: zhouguanya
9 | * @Date: 2019/01/14
10 | * @Description: 消息消费者
11 | */
12 | @Component
13 | public class MessageConsumer implements MessageListener {
14 |
15 | /**
16 | * 消费组监听消息
17 | */
18 | @Override
19 | public void onMessage(ConsumerRecord data) {
20 | System.out.printf("监听到消息:topic=%s,value=%s%n", data.topic(), data.value());
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/chapter16/src/main/java/com/test/kafka/producer/MessageProducer.java:
--------------------------------------------------------------------------------
1 | package com.test.kafka.producer;
2 |
3 | import org.springframework.beans.factory.annotation.Autowired;
4 | import org.springframework.kafka.core.KafkaTemplate;
5 | import org.springframework.stereotype.Component;
6 |
7 | /**
8 | * @Author: zhouguanya
9 | * @Date: 2019/01/14
10 | * @Description: 消息生产者
11 | */
12 | @Component
13 | public class MessageProducer {
14 | @Autowired
15 | private KafkaTemplate kafkaTemplate;
16 |
17 | /**
18 | * 发送消息
19 | * @param topic 主题
20 | * @param value 消息
21 | */
22 | public void send(String topic, String value) {
23 | kafkaTemplate.send(topic, value);
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/chapter16/src/test/java/com/test/kafka/SpringKafkaTest.java:
--------------------------------------------------------------------------------
1 | package com.test.kafka;
2 |
3 | import com.test.kafka.producer.MessageProducer;
4 | import org.junit.Test;
5 | import org.junit.runner.RunWith;
6 | import org.springframework.beans.factory.annotation.Autowired;
7 | import org.springframework.test.context.ContextConfiguration;
8 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
9 |
10 | /**
11 | * @Author: zhouguanya
12 | * @Date: 2019/01/14
13 | * @Description: 单元测试
14 | */
15 | @RunWith(SpringJUnit4ClassRunner.class)
16 | @ContextConfiguration("classpath:spring-kafka.xml")
17 | public class SpringKafkaTest {
18 | @Autowired
19 | private MessageProducer messageProducer;
20 | @Test
21 | public void sendMessage() throws InterruptedException {
22 | for (int i = 0; i < 10; i++) {
23 | messageProducer.send("spring-kafka-test", "Hello Kafka");
24 |
25 | }
26 | Thread.sleep(1000);
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/chapter17/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | project.demo
7 | com.test.spring5
8 | 1.0-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | chapter17
13 |
14 |
15 | com.alibaba
16 | fastjson
17 | 1.2.33
18 | test
19 |
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/chapter17/src/main/java/com/test/mycat/dao/CustomerDao.java:
--------------------------------------------------------------------------------
1 | package com.test.mycat.dao;
2 |
3 | import com.test.mycat.model.Customer;
4 |
5 | /**
6 | * @Author: zhouguanya
7 | * @Date: 2019/01/04
8 | * @Description:
9 | */
10 | public interface CustomerDao {
11 | int save(Customer customer);
12 | Customer query(int id);
13 | }
14 |
--------------------------------------------------------------------------------
/chapter17/src/main/java/com/test/mycat/dao/CustomerOrderDao.java:
--------------------------------------------------------------------------------
1 | package com.test.mycat.dao;
2 |
3 | import com.test.mycat.model.CustomerOrder;
4 |
5 | /**
6 | * @Author: zhouguanya
7 | * @Date: 2019/01/04
8 | * @Description:
9 | */
10 | public interface CustomerOrderDao {
11 | int save(CustomerOrder customer);
12 | CustomerOrder query(int id);
13 | }
14 |
--------------------------------------------------------------------------------
/chapter17/src/main/java/com/test/mycat/dao/ItemDao.java:
--------------------------------------------------------------------------------
1 | package com.test.mycat.dao;
2 |
3 | import com.test.mycat.model.Item;
4 |
5 | /**
6 | * @Author: zhouguanya
7 | * @Date: 2019/01/04
8 | * @Description:
9 | */
10 | public interface ItemDao {
11 | int save(Item customer);
12 | Item query(int id);
13 | }
14 |
--------------------------------------------------------------------------------
/chapter17/src/main/java/com/test/mycat/model/Item.java:
--------------------------------------------------------------------------------
1 | package com.test.mycat.model;
2 |
3 | import java.util.Date;
4 |
5 | /**
6 | * @Author: zhouguanya
7 | * @Date: 2019/01/04
8 | * @Description: 商品实体
9 | */
10 | public class Item {
11 | private int id;
12 | private int value;
13 | private Date addDate;
14 | private Date updateDate;
15 |
16 | public int getId() {
17 | return id;
18 | }
19 |
20 | public void setId(int id) {
21 | this.id = id;
22 | }
23 |
24 | public int getValue() {
25 | return value;
26 | }
27 |
28 | public void setValue(int value) {
29 | this.value = value;
30 | }
31 |
32 | public Date getAddDate() {
33 | return addDate;
34 | }
35 |
36 | public void setAddDate(Date addDate) {
37 | this.addDate = addDate;
38 | }
39 |
40 | public Date getUpdateDate() {
41 | return updateDate;
42 | }
43 |
44 | public void setUpdateDate(Date updateDate) {
45 | this.updateDate = updateDate;
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/chapter17/src/main/resources/jdbc.properties:
--------------------------------------------------------------------------------
1 | driver=com.mysql.jdbc.Driver
2 | #Mycat连接
3 | url=jdbc:mysql://127.0.0.1:8066/TESTDB
4 | #MySQL用户名
5 | username=root
6 | #MySQL密码
7 | password=123456
8 |
9 | idleConnectionTestPeriod=60
10 | maxIdleTime=240
11 | acquireIncrement=5
12 | initialPoolSize=10
13 | maxPoolSize=30
14 | minPoolSize=10
15 | autoCommitOnClose=false
16 | checkoutTimeout=1000
17 | acquireRetryAttempts=2
--------------------------------------------------------------------------------
/chapter18/src/main/java/com/test/sharding/config/DemoDatabaseShardingAlgorithm.java:
--------------------------------------------------------------------------------
1 | package com.test.sharding.config;
2 |
3 |
4 | import io.shardingjdbc.core.api.algorithm.sharding.PreciseShardingValue;
5 | import io.shardingjdbc.core.api.algorithm.sharding.standard.PreciseShardingAlgorithm;
6 |
7 | import java.util.Collection;
8 |
9 | /**
10 | * @Author: zhouguanya
11 | * @Date: 2019/01/19
12 | * @Description: 数据库分片的计算逻辑
13 | */
14 | public class DemoDatabaseShardingAlgorithm implements PreciseShardingAlgorithm {
15 | @Override
16 | public String doSharding(Collection collection, PreciseShardingValue preciseShardingValue) {
17 | for (String databaseName : collection) {
18 | //数据库后缀名
19 | String suffix = String.valueOf(preciseShardingValue.getValue() % 2);
20 | //如果数据库后缀 = suffix 则选择这个库
21 | if (databaseName.endsWith(suffix)) {
22 | return databaseName;
23 | }
24 | }
25 | throw new IllegalArgumentException("参数异常");
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/chapter18/src/main/java/com/test/sharding/config/DemoTableShardingAlgorithm.java:
--------------------------------------------------------------------------------
1 | package com.test.sharding.config;
2 |
3 |
4 | import io.shardingjdbc.core.api.algorithm.sharding.PreciseShardingValue;
5 | import io.shardingjdbc.core.api.algorithm.sharding.standard.PreciseShardingAlgorithm;
6 |
7 | import java.util.Collection;
8 |
9 | /**
10 | * @Author: zhouguanya
11 | * @Date: 2019/01/19
12 | * @Description: 数据表的分片规则
13 | */
14 | public class DemoTableShardingAlgorithm implements PreciseShardingAlgorithm {
15 | @Override
16 | public String doSharding(Collection collection, PreciseShardingValue preciseShardingValue) {
17 | for (String tableName : collection) {
18 | //表的后缀名
19 | String suffix = String.valueOf(preciseShardingValue.getValue() % 2);
20 | //如果表的后缀 = suffix 则选择这个表
21 | if (tableName.endsWith(suffix)) {
22 | return tableName;
23 | }
24 | }
25 | throw new IllegalArgumentException("参数异常");
26 | }
27 | }
--------------------------------------------------------------------------------
/chapter18/src/main/java/com/test/sharding/dao/ShopInfoDao.java:
--------------------------------------------------------------------------------
1 | package com.test.sharding.dao;
2 |
3 | import com.test.sharding.entity.ShopInfo;
4 |
5 | /**
6 | * @Author: zhouguanya
7 | * @Date: 2019/01/19
8 | * @Description: ShopInfo DAO层
9 | */
10 | public interface ShopInfoDao {
11 | /**
12 | * 保存商户
13 | */
14 | int insert(ShopInfo shopInfo);
15 |
16 | /**
17 | * 查询商户
18 | */
19 | ShopInfo selectByPrimaryKey(Long shopId);
20 |
21 | /**
22 | * 主键自增
23 | */
24 | int insertAutoIncrement(ShopInfo shopInfo);
25 | }
26 |
--------------------------------------------------------------------------------
/chapter18/src/main/java/com/test/sharding/entity/ShopInfo.java:
--------------------------------------------------------------------------------
1 | package com.test.sharding.entity;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/01/19
6 | * @Description: 商户信息
7 | */
8 | public class ShopInfo {
9 | /**
10 | * 商户id
11 | */
12 | private Long shopId;
13 |
14 | /**
15 | * 商户名
16 | */
17 | private String shopName;
18 |
19 | /**
20 | * 商户账户
21 | */
22 | private String account;
23 |
24 | public Long getShopId() {
25 | return shopId;
26 | }
27 |
28 | public void setShopId(Long shopId) {
29 | this.shopId = shopId;
30 | }
31 |
32 | public String getUserName() {
33 | return shopName;
34 | }
35 |
36 | public void setUserName(String userName) {
37 | this.shopName = userName;
38 | }
39 |
40 | public String getAccount() {
41 | return account;
42 | }
43 |
44 | public void setAccount(String account) {
45 | this.account = account;
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/chapter18/src/main/java/com/test/sharding/service/ShopService.java:
--------------------------------------------------------------------------------
1 | package com.test.sharding.service;
2 |
3 |
4 | import com.test.sharding.entity.ShopInfo;
5 | import com.test.sharding.dao.ShopInfoDao;
6 | import org.springframework.stereotype.Service;
7 |
8 | import javax.annotation.Resource;
9 |
10 | /**
11 | * @Author: zhouguanya
12 | * @Date: 2019/01/19
13 | * @Description: Service层
14 | */
15 | @Service
16 | public class ShopService {
17 |
18 | @Resource
19 | ShopInfoDao shopInfoDao;
20 |
21 | /**
22 | * 保存商户
23 | */
24 | public void saveShop(ShopInfo userInfo) {
25 | shopInfoDao.insert(userInfo);
26 | }
27 |
28 | /**
29 | * 查询商户
30 | */
31 | public ShopInfo queryShop(Long userId) {
32 | return shopInfoDao.selectByPrimaryKey(userId);
33 | }
34 |
35 | /**
36 | * 保存商户,主键自增
37 | */
38 | public void saveShopAutoIncrement(ShopInfo userInfo) {
39 | shopInfoDao.insertAutoIncrement(userInfo);
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/chapter18/src/main/resources/jdbc.properties:
--------------------------------------------------------------------------------
1 | driver=com.mysql.jdbc.Driver
2 | #Mycat连接
3 | url=jdbc:mysql://127.0.0.1:3306/TESTDB
4 | #MySQL用户名
5 | username=root
6 | #MySQL密码
7 | password=123456
8 |
9 | idleConnectionTestPeriod=60
10 | maxIdleTime=240
11 | acquireIncrement=5
12 | initialPoolSize=10
13 | maxPoolSize=30
14 | minPoolSize=10
15 | autoCommitOnClose=false
16 | checkoutTimeout=1000
17 | acquireRetryAttempts=2
--------------------------------------------------------------------------------
/chapter19/api/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | chapter19
7 | com.test.spring5
8 | 1.0-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | dubbo-api
13 |
14 |
15 |
--------------------------------------------------------------------------------
/chapter19/api/src/main/java/com/test/dubbo/api/HelloService.java:
--------------------------------------------------------------------------------
1 | package com.test.dubbo.api;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/01/31
6 | * @Description: api
7 | */
8 | public interface HelloService {
9 | /**
10 | * 抽象方法
11 | * @param name
12 | * @return
13 | */
14 | String sayHello(String name);
15 | }
16 |
--------------------------------------------------------------------------------
/chapter19/consumer/src/main/resources/dubbo-consumer.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/chapter19/consumer/src/test/java/com/test/dubbo/consumer/DubboConsumerTest.java:
--------------------------------------------------------------------------------
1 | package com.test.dubbo.consumer;
2 |
3 | import com.test.dubbo.api.HelloService;
4 | import org.junit.Test;
5 | import org.junit.runner.RunWith;
6 | import org.springframework.beans.factory.annotation.Autowired;
7 | import org.springframework.test.context.ContextConfiguration;
8 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
9 |
10 | /**
11 | * @Author: zhouguanya
12 | * @Date: 2019/01/31
13 | * @Description: 测试服务调用者
14 | */
15 | @RunWith(SpringJUnit4ClassRunner.class)
16 | @ContextConfiguration("classpath:dubbo-consumer.xml")
17 | public class DubboConsumerTest {
18 | @Autowired
19 | private HelloService helloService;
20 | @Test
21 | public void test() throws InterruptedException {
22 | for (int i = 0; i < 10; i++) {
23 | Thread.sleep(1000);
24 | System.out.println(helloService.sayHello("Michael"));
25 | }
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/chapter19/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | project.demo
7 | com.test.spring5
8 | 1.0-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | chapter19
13 | pom
14 |
15 | api
16 | consumer
17 | provider
18 | spi
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/chapter19/provider/src/main/java/com/test/dubbo/provider/HelloServiceImpl.java:
--------------------------------------------------------------------------------
1 | package com.test.dubbo.provider;
2 |
3 | import com.alibaba.dubbo.rpc.RpcContext;
4 | import com.test.dubbo.api.HelloService;
5 |
6 | import java.time.LocalDateTime;
7 |
8 | /**
9 | * @Author: zhouguanya
10 | * @Date: 2019/01/31
11 | * @Description: 服务提供方实现接口
12 | */
13 | public class HelloServiceImpl implements HelloService {
14 | /**
15 | * sayHello方法实现
16 | */
17 | @Override
18 | public String sayHello(String name) {
19 | System.out.println(LocalDateTime.now() + " hello" + name + ", response from provider: " + RpcContext.getContext().getLocalAddress());
20 | return "hello " + name;
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/chapter19/provider/src/main/resources/dubbo-provider.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
--------------------------------------------------------------------------------
/chapter19/provider/src/test/java/com/test/dubbo/provider/DubboProviderTest.java:
--------------------------------------------------------------------------------
1 | package com.test.dubbo.provider;
2 |
3 | import org.junit.Test;
4 | import org.junit.runner.RunWith;
5 | import org.springframework.test.context.ContextConfiguration;
6 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
7 |
8 | /**
9 | * @Author: zhouguanya
10 | * @Date: 2019/01/31
11 | * @Description:
12 | */
13 | @RunWith(SpringJUnit4ClassRunner.class)
14 | @ContextConfiguration("classpath:dubbo-provider.xml")
15 | public class DubboProviderTest {
16 | @Test
17 | public void startProvider() throws Exception {
18 | System.out.println("Dubbo Provider started successfully...");
19 | System.in.read();
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/chapter19/spi/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | chapter19
7 | com.test.spring5
8 | 1.0-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | spi
13 |
14 |
15 |
--------------------------------------------------------------------------------
/chapter19/spi/src/main/java/com/test/ChineseSpiHelloServiceImpl.java:
--------------------------------------------------------------------------------
1 | package com.test;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019-02-04
6 | * @Description:
7 | */
8 | public class ChineseSpiHelloServiceImpl implements SpiHelloService {
9 | public void say() {
10 | System.out.println("世界,你好");
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/chapter19/spi/src/main/java/com/test/EnglishSpiHelloServiceImpl.java:
--------------------------------------------------------------------------------
1 | package com.test;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019-02-04
6 | * @Description:
7 | */
8 | public class EnglishSpiHelloServiceImpl implements SpiHelloService {
9 | public void say() {
10 | System.out.println("Hello World");
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/chapter19/spi/src/main/java/com/test/SpiDemo.java:
--------------------------------------------------------------------------------
1 | package com.test;
2 |
3 | import java.util.ServiceLoader;
4 |
5 | /**
6 | * @Author: zhouguanya
7 | * @Date: 2019-02-04
8 | * @Description:
9 | */
10 | public class SpiDemo {
11 | public static void main(String[] args) {
12 | ServiceLoader services = ServiceLoader.load(SpiHelloService.class);
13 | for (SpiHelloService service : services) {
14 | service.say();
15 | }
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/chapter19/spi/src/main/java/com/test/SpiHelloService.java:
--------------------------------------------------------------------------------
1 | package com.test;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019-02-04
6 | * @Description:
7 | */
8 | public interface SpiHelloService {
9 | void say();
10 | }
11 |
--------------------------------------------------------------------------------
/chapter19/spi/src/main/resources/META-INF/services/com.test.SpiHelloService:
--------------------------------------------------------------------------------
1 | com.test.EnglishSpiHelloServiceImpl
2 | com.test.ChineseSpiHelloServiceImpl
--------------------------------------------------------------------------------
/chapter2/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | project.demo
7 | com.test.spring5
8 | 1.0-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | chapter2
13 |
--------------------------------------------------------------------------------
/chapter2/src/main/java/com/test/ioc/BeanFactory创建流程图.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/online-demo/spring5projectdemo/4574663109e7e0d732733b6f694f211ba5ed7ba3/chapter2/src/main/java/com/test/ioc/BeanFactory创建流程图.png
--------------------------------------------------------------------------------
/chapter2/src/main/java/com/test/ioc/annotation/HomeWork.java:
--------------------------------------------------------------------------------
1 | package com.test.ioc.annotation;
2 |
3 | public interface HomeWork {
4 | /**
5 | * 写家庭作业
6 | */
7 | void doHomeWork();
8 | }
9 |
--------------------------------------------------------------------------------
/chapter2/src/main/java/com/test/ioc/annotation/Student.java:
--------------------------------------------------------------------------------
1 | package com.test.ioc.annotation;
2 |
3 | import org.springframework.stereotype.Service;
4 |
5 | @Service
6 | public class Student implements HomeWork {
7 | /**
8 | * 写家庭作业
9 | */
10 | @Override
11 | public void doHomeWork() {
12 | System.out.println("我是学生,我要写家庭作业");
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/chapter2/src/main/java/com/test/ioc/annotation/readme.txt:
--------------------------------------------------------------------------------
1 | 注解方式实现IoC:
2 | @Service注解实现Student的IoC
3 |
--------------------------------------------------------------------------------
/chapter2/src/main/java/com/test/ioc/readme.txt:
--------------------------------------------------------------------------------
1 | IOC的实现方式
2 | # XML配置方式
3 | # 注解方式
--------------------------------------------------------------------------------
/chapter2/src/main/java/com/test/ioc/xml/Deliverable.java:
--------------------------------------------------------------------------------
1 | package com.test.ioc.xml;
2 |
3 | public interface Deliverable {
4 | /**
5 | * 发货
6 | */
7 | void delivery();
8 | }
9 |
--------------------------------------------------------------------------------
/chapter2/src/main/java/com/test/ioc/xml/Order.java:
--------------------------------------------------------------------------------
1 | package com.test.ioc.xml;
2 |
3 | /**
4 | * 有参构造器实现IoC
5 | */
6 | public class Order implements Deliverable {
7 | /**
8 | * 订单号
9 | */
10 | private long orderId;
11 | /**
12 | * 订单金额
13 | */
14 | private double amount;
15 |
16 | /**
17 | * 有参构造器
18 | * @param orderId
19 | * @param amount
20 | */
21 | public Order (long orderId, double amount) {
22 | this.orderId = orderId;
23 | this.amount = amount;
24 | }
25 |
26 | /**
27 | * 订单发货方法
28 | */
29 | @Override
30 | public void delivery() {
31 | System.out.printf("订单号%s,金额%s,已发货!", orderId, amount);
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/chapter2/src/main/java/com/test/ioc/xml/Speakable.java:
--------------------------------------------------------------------------------
1 | package com.test.ioc.xml;
2 |
3 | public interface Speakable {
4 | /**
5 | * 说话
6 | */
7 | void say();
8 | }
9 |
--------------------------------------------------------------------------------
/chapter2/src/main/java/com/test/ioc/xml/User.java:
--------------------------------------------------------------------------------
1 | package com.test.ioc.xml;
2 |
3 | /**
4 | * 无参构造器实现IoC
5 | */
6 | public class User implements Speakable {
7 | /**
8 | * 无参构造器
9 | */
10 | public User () {
11 |
12 | }
13 |
14 | /**
15 | * 说话的方法
16 | */
17 | @Override
18 | public void say() {
19 | System.out.println("大家好");
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/chapter2/src/main/java/com/test/ioc/xml/readme.txt:
--------------------------------------------------------------------------------
1 | XML方式实现IoC:
2 | User 无参构造器
3 |
4 | Order带有两个参数的构造器
--------------------------------------------------------------------------------
/chapter2/src/main/java/com/test/lifecycle/beanlifcycle/readme.txt:
--------------------------------------------------------------------------------
1 | Bean生命周期
--------------------------------------------------------------------------------
/chapter2/src/main/java/com/test/lifecycle/containerlifecycle/readme.txt:
--------------------------------------------------------------------------------
1 | 容器级生命周期接口方法的执行时序
--------------------------------------------------------------------------------
/chapter2/src/main/java/com/test/lifecycle/factorylifecycle/FactoryLifecycle.java:
--------------------------------------------------------------------------------
1 | package com.test.lifecycle.factorylifecycle;
2 |
3 | import org.springframework.beans.BeansException;
4 | import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
5 | import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
6 |
7 | /**
8 | * @Author zhouguanya
9 | * @Date 2018/8/19
10 | * @Description 工厂级生命周期
11 | */
12 | public class FactoryLifecycle implements BeanFactoryPostProcessor {
13 |
14 | /**
15 | * 构造器
16 | */
17 | public FactoryLifecycle () {
18 | System.out.println("一 【工厂级别】FactoryLifecycle构造器执行了");
19 | }
20 |
21 | /**
22 | * Bean实例化之前
23 | */
24 | @Override
25 | public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
26 | System.out.println("二 【工厂级别】postProcessBeanFactory方法执行了");
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/chapter2/src/main/java/com/test/lifecycle/factorylifecycle/readme.txt:
--------------------------------------------------------------------------------
1 | 工厂级级生命周期接口方法的执行时序
--------------------------------------------------------------------------------
/chapter2/src/main/java/com/test/sourcecodelearning/BookService.java:
--------------------------------------------------------------------------------
1 | package com.test.sourcecodelearning;
2 |
3 | /**
4 | * @Author zhouguanya
5 | * @Date 2018/8/17
6 | * @Description
7 | */
8 | public interface BookService {
9 | /**
10 | * 书的售价
11 | * @return
12 | */
13 | double getBookPrice();
14 | }
15 |
--------------------------------------------------------------------------------
/chapter2/src/main/java/com/test/sourcecodelearning/BookServiceImpl.java:
--------------------------------------------------------------------------------
1 | package com.test.sourcecodelearning;
2 |
3 | /**
4 | * @Author zhouguanya
5 | * @Date 2018/8/17
6 | * @Description 图书
7 | */
8 | public class BookServiceImpl implements BookService {
9 | @Override
10 | public double getBookPrice() {
11 | return 508.8;
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/chapter2/src/main/java/com/test/sourcecodelearning/PressService.java:
--------------------------------------------------------------------------------
1 | package com.test.sourcecodelearning;
2 |
3 | /**
4 | * @Author zhouguanya
5 | * @Date 2018/8/17
6 | * @Description
7 | */
8 | public interface PressService {
9 | /**
10 | * 返回一句话
11 | * @return
12 | */
13 | String say();
14 | }
15 |
--------------------------------------------------------------------------------
/chapter2/src/main/java/com/test/sourcecodelearning/PressServiceImpl.java:
--------------------------------------------------------------------------------
1 | package com.test.sourcecodelearning;
2 |
3 | /**
4 | * @Author zhouguanya
5 | * @Date 2018/8/17
6 | * @Description 出版社类
7 | */
8 | public class PressServiceImpl implements PressService {
9 | /**
10 | * 依赖BookService
11 | */
12 | private BookService bookService;
13 |
14 | /**
15 | * 依赖注入的地方
16 | * @param bookService
17 | */
18 | public void setBookService(BookService bookService) {
19 | this.bookService = bookService;
20 | }
21 |
22 | @Override
23 | public String say() {
24 | return "本书的价格是:" + bookService.getBookPrice();
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/chapter2/src/main/java/com/test/sourcecodelearning/SourceCodeLearning.java:
--------------------------------------------------------------------------------
1 | package com.test.sourcecodelearning;
2 |
3 | import org.springframework.context.ApplicationContext;
4 | import org.springframework.context.support.FileSystemXmlApplicationContext;
5 |
6 | /**
7 | * @Author zhouguanya
8 | * @Date 2018/8/14
9 | * @Description IoC源码解析
10 | */
11 | public class SourceCodeLearning {
12 | public static void main(String[] args) {
13 | //ApplicationContext:Spring的上下文。通过对源码的类的集成关系可以看出,
14 | // FileSystemXmlApplicationContext是ApplicationContext的一个标准实现
15 | ApplicationContext applicationContext =
16 | new FileSystemXmlApplicationContext("classpath:spring-chapter2.xml");
17 | //从容器中获取名字为user的bean
18 | PressService pressService =
19 | (PressService) applicationContext.getBean("pressService");
20 | //调用bean的方法
21 | String price = pressService.say();
22 | System.out.println(price);
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/chapter2/src/main/java/com/test/sourcecodelearning/readme.txt:
--------------------------------------------------------------------------------
1 | IOC 源码解析
--------------------------------------------------------------------------------
/chapter2/src/main/resources/spring-chapter2-beanlifecycle.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/chapter2/src/main/resources/spring-chapter2-containerlifecycle.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/chapter2/src/main/resources/spring-chapter2-factorybeanlifecycle.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/chapter2/src/test/java/ioc/annotation/AnnotationTest.java:
--------------------------------------------------------------------------------
1 | package ioc.annotation;
2 |
3 | import com.test.ioc.annotation.HomeWork;
4 | import org.junit.Test;
5 | import org.junit.runner.RunWith;
6 | import org.springframework.beans.factory.annotation.Autowired;
7 | import org.springframework.test.context.ContextConfiguration;
8 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
9 | import org.springframework.test.context.junit4.SpringRunner;
10 |
11 | /**
12 | * 测试注解方式的IoC
13 | */
14 | @RunWith(SpringRunner.class)
15 | @ContextConfiguration("classpath:spring-chapter2.xml")
16 | public class AnnotationTest {
17 | @Autowired
18 | private HomeWork homeWork;
19 | //Spring 容器注入依赖的Deliverable对象
20 | @Test
21 | public void test() {
22 | homeWork.doHomeWork();
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/chapter2/src/test/java/ioc/annotation/readme.txt:
--------------------------------------------------------------------------------
1 | 测试注解方式实现的IoC
2 |
--------------------------------------------------------------------------------
/chapter2/src/test/java/ioc/xml/XmlTest.java:
--------------------------------------------------------------------------------
1 | package ioc.xml;
2 |
3 | import com.test.ioc.xml.Deliverable;
4 | import com.test.ioc.xml.Speakable;
5 | import org.junit.Test;
6 | import org.junit.runner.RunWith;
7 | import org.springframework.beans.factory.annotation.Autowired;
8 | import org.springframework.test.context.ContextConfiguration;
9 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
10 |
11 | /**
12 | * 测试XML方式的IoC
13 | */
14 | @RunWith(SpringJUnit4ClassRunner.class)
15 | @ContextConfiguration("classpath:spring-chapter2.xml")
16 | public class XmlTest {
17 | //Spring 容器注入依赖的Speakable对象
18 | @Autowired
19 | private Speakable speakable;
20 | //Spring 容器注入依赖的Deliverable对象
21 | @Autowired
22 | private Deliverable deliverable;
23 | @Test
24 | public void test() {
25 | speakable.say();
26 | deliverable.delivery();
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/chapter2/src/test/java/ioc/xml/readme.txt:
--------------------------------------------------------------------------------
1 | 测试XML方式实现的IoC
2 |
--------------------------------------------------------------------------------
/chapter2/src/test/java/lifecycle/beanlifcycle/BeanLifecycleTest.java:
--------------------------------------------------------------------------------
1 | package lifecycle.beanlifcycle;
2 |
3 | import com.test.lifecycle.beanlifcycle.BeanLifecycle;
4 | import org.junit.Test;
5 | import org.junit.runner.RunWith;
6 | import org.springframework.beans.factory.annotation.Autowired;
7 | import org.springframework.test.context.ContextConfiguration;
8 | import org.springframework.test.context.junit4.SpringRunner;
9 |
10 | /**
11 | * @Author zhouguanya
12 | * @Date 2018/8/18
13 | * @Description Bean生命周期测试
14 | */
15 | @RunWith(SpringRunner.class)
16 | @ContextConfiguration("classpath:spring-chapter2-beanlifecycle.xml")
17 | public class BeanLifecycleTest {
18 | @Autowired
19 | private BeanLifecycle beanLifecycle;
20 |
21 | @Test
22 | public void test() {
23 | beanLifecycle.sayHello();
24 | }
25 |
26 | }
27 |
--------------------------------------------------------------------------------
/chapter2/src/test/java/lifecycle/factorylifecycle/FactoryLifecycleTest.java:
--------------------------------------------------------------------------------
1 | package lifecycle.factorylifecycle;
2 |
3 | import com.test.lifecycle.beanlifcycle.BeanLifecycle;
4 | import org.springframework.context.support.ClassPathXmlApplicationContext;
5 |
6 | /**
7 | * @Author zhouguanya
8 | * @Date 2018/8/19
9 | * @Description Bean级生命周期 + 容器级生命周期 + 工厂级生命周期测试
10 | */
11 | public class FactoryLifecycleTest {
12 | public static void main(String[] args) {
13 | ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
14 | "classpath:spring-chapter2-beanlifecycle.xml"
15 | ,"classpath:spring-chapter2-containerlifecycle.xml", "classpath:spring-chapter2-factorybeanlifecycle.xml");
16 | BeanLifecycle beanLifecycle = context.getBean("beanLifecycle",BeanLifecycle.class);
17 | beanLifecycle.sayHello();
18 | context.close();
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/chapter20/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | project.demo
7 | com.test.spring5
8 | 1.0-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | chapter20
13 |
14 |
15 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/abstractfactory/AbstractFactory.java:
--------------------------------------------------------------------------------
1 | package com.test.abstractfactory;
2 |
3 | import com.test.common.Computer;
4 |
5 | /**
6 | * @Author: zhouguanya
7 | * @Date: 2019/02/05
8 | * @Description: 抽象工厂
9 | */
10 | public abstract class AbstractFactory {
11 | /**
12 | * 创建电脑
13 | */
14 | abstract Computer createComputer(String type);
15 | /**
16 | * 制定电脑价格
17 | */
18 | abstract Price createPrice(String currency);
19 | }
20 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/abstractfactory/Dollar.java:
--------------------------------------------------------------------------------
1 | package com.test.abstractfactory;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/05
6 | * @Description:
7 | */
8 | public class Dollar implements Price {
9 | /**
10 | * 设置价格
11 | */
12 | @Override
13 | public void setPrice() {
14 | System.out.println("制定电脑的美元售价");
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/abstractfactory/FactoryProducer.java:
--------------------------------------------------------------------------------
1 | package com.test.abstractfactory;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/05
6 | * @Description: 工厂生成器
7 | */
8 | public class FactoryProducer {
9 | public static AbstractFactory getFactory(String factoryType) {
10 | if ("Computer".equals(factoryType)) {
11 | return new ComputerFactory();
12 | } else if ("Price".equals(factoryType)) {
13 | return new PriceFactory();
14 | }
15 | return null;
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/abstractfactory/Pound.java:
--------------------------------------------------------------------------------
1 | package com.test.abstractfactory;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/05
6 | * @Description:
7 | */
8 | public class Pound implements Price {
9 | /**
10 | * 设置价格
11 | */
12 | @Override
13 | public void setPrice() {
14 | System.out.println("制定电脑的英镑售价");
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/abstractfactory/Price.java:
--------------------------------------------------------------------------------
1 | package com.test.abstractfactory;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/05
6 | * @Description: 价格接口
7 | */
8 | public interface Price {
9 | /**
10 | * 设置价格
11 | */
12 | void setPrice();
13 | }
14 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/abstractfactory/PriceFactory.java:
--------------------------------------------------------------------------------
1 | package com.test.abstractfactory;
2 |
3 | import com.test.common.Computer;
4 |
5 | /**
6 | * @Author: zhouguanya
7 | * @Date: 2019/02/05
8 | * @Description: 价格工厂
9 | */
10 | public class PriceFactory extends AbstractFactory{
11 | /**
12 | * 创建电脑
13 | */
14 | @Override
15 | Computer createComputer(String type) {
16 | return null;
17 | }
18 |
19 | /**
20 | * 制定电脑价格
21 | */
22 | @Override
23 | Price createPrice(String currency) {
24 | if (currency == null || currency.equals("")) {
25 | return null;
26 | }
27 | switch (currency) {
28 | case "RMB":
29 | return new RMB();
30 | case "Dollar":
31 | return new Dollar();
32 | case "Pound":
33 | return new Pound();
34 | default:
35 | return null;
36 | }
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/abstractfactory/RMB.java:
--------------------------------------------------------------------------------
1 | package com.test.abstractfactory;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/05
6 | * @Description:
7 | */
8 | public class RMB implements Price {
9 | /**
10 | * 设置价格
11 | */
12 | @Override
13 | public void setPrice() {
14 | System.out.println("制定电脑的人民币售价");
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/adapter/AdapterPatternDemo.java:
--------------------------------------------------------------------------------
1 | package com.test.adapter;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/06
6 | * @Description: 适配器模式测试类
7 | */
8 | public class AdapterPatternDemo {
9 | public static void main(String[] args) {
10 | AudioPlay audioPlay = new AudioPlay();
11 | audioPlay.play("MP3");
12 | audioPlay.play("MP4");
13 | audioPlay.play("RMVB");
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/adapter/AdvancedPlayer.java:
--------------------------------------------------------------------------------
1 | package com.test.adapter;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/06
6 | * @Description: 高级播放器接口
7 | */
8 | public interface AdvancedPlayer {
9 | /**
10 | * 播放MP4
11 | */
12 | void playMp4();
13 | /**
14 | * 播放RMVB
15 | */
16 | void playRMVB();
17 | }
18 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/adapter/AudioPlay.java:
--------------------------------------------------------------------------------
1 | package com.test.adapter;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/06
6 | * @Description: 音频播放器
7 | */
8 | public class AudioPlay implements Player {
9 | PlayAdapter playAdapter;
10 |
11 | @Override
12 | public void play(String type) {
13 | if ("MP3".equals(type)) {
14 | System.out.println("播放MP3格式的文件");
15 | } else if ("MP4".equals(type) || "RMVB".equals(type)) {
16 | playAdapter = new PlayAdapter(type);
17 | playAdapter.play(type);
18 | }
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/adapter/Mp4Play.java:
--------------------------------------------------------------------------------
1 | package com.test.adapter;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/06
6 | * @Description: MP4格式播放器
7 | */
8 | public class Mp4Play implements AdvancedPlayer {
9 | /**
10 | * 播放MP4
11 | */
12 | @Override
13 | public void playMp4() {
14 | System.out.println("播放MP4格式的文件");
15 | }
16 |
17 | /**
18 | * 播放RMVB
19 | */
20 | @Override
21 | public void playRMVB() {
22 |
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/adapter/PlayAdapter.java:
--------------------------------------------------------------------------------
1 | package com.test.adapter;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/06
6 | * @Description: 播放器适配器
7 | */
8 | public class PlayAdapter implements Player {
9 | private AdvancedPlayer advancedPlayer;
10 |
11 | public PlayAdapter (String type) {
12 | if ("MP4".equals(type)) {
13 | advancedPlayer = new Mp4Play();
14 | } else if ("RMVB".equals(type)) {
15 | advancedPlayer = new RmvbPlay();
16 | }
17 | }
18 |
19 | @Override
20 | public void play(String type) {
21 | if ("MP4".equals(type)) {
22 | advancedPlayer.playMp4();
23 | } else if ("RMVB".equals(type)) {
24 | advancedPlayer.playRMVB();
25 | }
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/adapter/Player.java:
--------------------------------------------------------------------------------
1 | package com.test.adapter;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/06
6 | * @Description: 播放器接口
7 | */
8 | public interface Player {
9 | void play(String type);
10 | }
11 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/adapter/RmvbPlay.java:
--------------------------------------------------------------------------------
1 | package com.test.adapter;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/06
6 | * @Description: RMVB格式播放器
7 | */
8 | public class RmvbPlay implements AdvancedPlayer {
9 | /**
10 | * 播放MP4
11 | */
12 | @Override
13 | public void playMp4() {
14 |
15 | }
16 |
17 | /**
18 | * 播放RMVB
19 | */
20 | @Override
21 | public void playRMVB() {
22 | System.out.println("播放RMVB格式的文件");
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/bridge/Bridge.java:
--------------------------------------------------------------------------------
1 | package com.test.bridge;
2 |
3 | import com.test.common.Computer;
4 |
5 | /**
6 | * @Author: zhouguanya
7 | * @Date: 2019/02/06
8 | * @Description: 桥接
9 | */
10 | public abstract class Bridge {
11 | protected Computer computer;
12 | public Bridge(Computer computer) {
13 | this.computer = computer;
14 | }
15 |
16 | public abstract void makeComputer();
17 | }
18 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/bridge/BridgePatternDemo.java:
--------------------------------------------------------------------------------
1 | package com.test.bridge;
2 |
3 | import com.test.common.ASUS;
4 | import com.test.common.Lenovo;
5 | import com.test.common.MacBook;
6 |
7 | /**
8 | * @Author: zhouguanya
9 | * @Date: 2019/02/06
10 | * @Description: 桥接设计模式测试
11 | */
12 | public class BridgePatternDemo {
13 | public static void main(String[] args) {
14 | ComputerBridge asus = new ComputerBridge(new ASUS());
15 | asus.makeComputer();
16 | ComputerBridge lenove = new ComputerBridge(new Lenovo());
17 | lenove.makeComputer();
18 | ComputerBridge macBook = new ComputerBridge(new MacBook());
19 | macBook.makeComputer();
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/bridge/ComputerBridge.java:
--------------------------------------------------------------------------------
1 | package com.test.bridge;
2 |
3 | import com.test.common.Computer;
4 |
5 | /**
6 | * @Author: zhouguanya
7 | * @Date: 2019/02/06
8 | * @Description:
9 | */
10 | public class ComputerBridge extends Bridge {
11 |
12 | public ComputerBridge(Computer computer) {
13 | super(computer);
14 | }
15 |
16 | @Override
17 | public void makeComputer() {
18 | computer.make();
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/builder/AMD.java:
--------------------------------------------------------------------------------
1 | package com.test.builder;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/06
6 | * @Description:
7 | */
8 | public class AMD extends CPU {
9 | @Override
10 | public String name() {
11 | return "AMD处理器";
12 | }
13 |
14 | @Override
15 | public int price() {
16 | return 2000;
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/builder/BuilderPattenDemo.java:
--------------------------------------------------------------------------------
1 | package com.test.builder;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/06
6 | * @Description: 建造者模式测试
7 | */
8 | public class BuilderPattenDemo {
9 | public static void main(String[] args) {
10 | ComputerBuilder computerBuilder = new ComputerBuilder();
11 | Computer cheapComputer = computerBuilder.buildCheap();
12 | cheapComputer.showItems();
13 | ComputerBuilder expensiveBuilder = new ComputerBuilder();
14 | Computer expensiveComputer = expensiveBuilder.buildExpensive();
15 | expensiveComputer.showItems();
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/builder/CPU.java:
--------------------------------------------------------------------------------
1 | package com.test.builder;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/06
6 | * @Description: CPU
7 | */
8 | public abstract class CPU implements Item {
9 |
10 | @Override
11 | public Packing packing() {
12 | return new Wrapper();
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/builder/Computer.java:
--------------------------------------------------------------------------------
1 | package com.test.builder;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | /**
7 | * @Author: zhouguanya
8 | * @Date: 2019/02/06
9 | * @Description: 电脑类
10 | */
11 | public class Computer {
12 | private List- itemList = new ArrayList<>();
13 |
14 | public void addItem(Item item){
15 | itemList.add(item);
16 | }
17 |
18 | public void showItems() {
19 | int total = 0;
20 | for (Item item : itemList) {
21 | System.out.print(item.packing().pack() + item.name() + ",价格=" + item.price() + "\t");
22 | total += item.price();
23 | }
24 | System.out.println("电脑总价=" + total);
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/builder/ComputerBuilder.java:
--------------------------------------------------------------------------------
1 | package com.test.builder;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/06
6 | * @Description: 电脑建造者
7 | */
8 | public class ComputerBuilder {
9 |
10 | /**
11 | * 创建廉价电脑
12 | */
13 | public Computer buildCheap() {
14 | Computer computer = new Computer();
15 | computer.addItem(new AMD());
16 | computer.addItem(new PHILIPS());
17 | return computer;
18 | }
19 |
20 | /**
21 | * 创建高价电脑
22 | */
23 | public Computer buildExpensive() {
24 | Computer computer = new Computer();
25 | computer.addItem(new Intel());
26 | computer.addItem(new DELL());
27 | return computer;
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/builder/DELL.java:
--------------------------------------------------------------------------------
1 | package com.test.builder;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/06
6 | * @Description:
7 | */
8 | public class DELL extends Screen {
9 | @Override
10 | public String name() {
11 | return "DELL显示器";
12 | }
13 |
14 | @Override
15 | public int price() {
16 | return 2000;
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/builder/Intel.java:
--------------------------------------------------------------------------------
1 | package com.test.builder;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/06
6 | * @Description:
7 | */
8 | public class Intel extends CPU {
9 | @Override
10 | public String name() {
11 | return "Intel处理器";
12 | }
13 |
14 | @Override
15 | public int price() {
16 | return 3000;
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/builder/Item.java:
--------------------------------------------------------------------------------
1 | package com.test.builder;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/06
6 | * @Description: 电脑配件接口
7 | */
8 | public interface Item {
9 | String name();
10 | Packing packing();
11 | int price();
12 | }
13 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/builder/PHILIPS.java:
--------------------------------------------------------------------------------
1 | package com.test.builder;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/06
6 | * @Description:
7 | */
8 | public class PHILIPS extends Screen {
9 | @Override
10 | public String name() {
11 | return "PHILIPS显示器";
12 | }
13 |
14 | @Override
15 | public int price() {
16 | return 1000;
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/builder/Packing.java:
--------------------------------------------------------------------------------
1 | package com.test.builder;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/06
6 | * @Description: 组装接口
7 | */
8 | public interface Packing {
9 | String pack();
10 | }
11 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/builder/Screen.java:
--------------------------------------------------------------------------------
1 | package com.test.builder;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/06
6 | * @Description: 显示器
7 | */
8 | public abstract class Screen implements Item {
9 |
10 | @Override
11 | public Packing packing() {
12 | return new Wrapper();
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/builder/Wrapper.java:
--------------------------------------------------------------------------------
1 | package com.test.builder;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/06
6 | * @Description: 组装
7 | */
8 | public class Wrapper implements Packing {
9 | @Override
10 | public String pack() {
11 | return "组装";
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/chain/AbstractFilter.java:
--------------------------------------------------------------------------------
1 | package com.test.chain;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/07
6 | * @Description:
7 | */
8 | public abstract class AbstractFilter {
9 | AbstractFilter nextFilter;
10 | //责任链中的下一个元素
11 | public void setNextFilter(AbstractFilter nextFilter) {
12 | this.nextFilter = nextFilter;
13 | }
14 |
15 | public String filter(String content) {
16 | String filtered = doFilter(content);
17 | if (nextFilter != null) {
18 | return nextFilter.filter(filtered);
19 | }
20 | return filtered;
21 | }
22 |
23 | protected abstract String doFilter(String content);
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/chain/ChainPatternDemo.java:
--------------------------------------------------------------------------------
1 | package com.test.chain;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/07
6 | * @Description: 责任链模式测试类
7 | */
8 | public class ChainPatternDemo {
9 | static String content = "Dirty Word, Privacy Word, Sensitive Word";
10 | public static void main(String[] args) {
11 | AbstractFilter filterChain = getFilterChain();
12 | System.out.println(filterChain.filter(content));
13 | }
14 | private static AbstractFilter getFilterChain() {
15 | DirtyFilter dirtyFilter = new DirtyFilter();
16 | PrivacyFilter privacyFilter = new PrivacyFilter();
17 | SensitiveFilter sensitiveFilter = new SensitiveFilter();
18 | dirtyFilter.setNextFilter(privacyFilter);
19 | privacyFilter.setNextFilter(sensitiveFilter);
20 | sensitiveFilter.setNextFilter(null);
21 | return dirtyFilter;
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/chain/DirtyFilter.java:
--------------------------------------------------------------------------------
1 | package com.test.chain;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/07
6 | * @Description:
7 | */
8 | public class DirtyFilter extends AbstractFilter {
9 | @Override
10 | protected String doFilter(String content) {
11 | return content.replace("Dirty Word","dw");
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/chain/PrivacyFilter.java:
--------------------------------------------------------------------------------
1 | package com.test.chain;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/07
6 | * @Description:
7 | */
8 | public class PrivacyFilter extends AbstractFilter {
9 | @Override
10 | protected String doFilter(String content) {
11 | return content.replace("Privacy Word", "pw");
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/chain/SensitiveFilter.java:
--------------------------------------------------------------------------------
1 | package com.test.chain;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/07
6 | * @Description:
7 | */
8 | public class SensitiveFilter extends AbstractFilter {
9 | @Override
10 | protected String doFilter(String content) {
11 | return content.replace("Sensitive Word", "sw");
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/command/AttentionCommand.java:
--------------------------------------------------------------------------------
1 | package com.test.command;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/07
6 | * @Description: 立正命令
7 | */
8 | public class AttentionCommand implements Command {
9 | private Soldier soldier;
10 |
11 | public AttentionCommand(Soldier soldier) {
12 | this.soldier = soldier;
13 | }
14 |
15 | @Override
16 | public void doCommand() {
17 | soldier.attention();
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/command/Broker.java:
--------------------------------------------------------------------------------
1 | package com.test.command;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | /**
7 | * @Author: zhouguanya
8 | * @Date: 2019/02/07
9 | * @Description: 命令的调用类
10 | */
11 | public class Broker {
12 | private List commandList = new ArrayList<>();
13 |
14 | /**
15 | * 添加命令
16 | */
17 | public void addCommand(Command command) {
18 | commandList.add(command);
19 | }
20 |
21 | /**
22 | * 执行命令
23 | */
24 | public void executeCommand() {
25 | for (Command command : commandList) {
26 | command.doCommand();
27 | }
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/command/Command.java:
--------------------------------------------------------------------------------
1 | package com.test.command;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/07
6 | * @Description:
7 | */
8 | public interface Command {
9 | void doCommand();
10 | }
11 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/command/CommandPatternDemo.java:
--------------------------------------------------------------------------------
1 | package com.test.command;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/07
6 | * @Description: 命令模式测试类
7 | */
8 | public class CommandPatternDemo {
9 | public static void main(String[] args) {
10 | Soldier soldier = new Soldier();
11 | AttentionCommand attentionCommand = new AttentionCommand(soldier);
12 | StandEaseCommand standEaseCommand = new StandEaseCommand(soldier);
13 | Broker broker = new Broker();
14 | broker.addCommand(attentionCommand);
15 | broker.addCommand(standEaseCommand);
16 | broker.executeCommand();
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/command/Soldier.java:
--------------------------------------------------------------------------------
1 | package com.test.command;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/07
6 | * @Description: 士兵
7 | */
8 | public class Soldier {
9 |
10 | public void attention() {
11 | System.out.println("立正");
12 | }
13 |
14 | public void standAtEase() {
15 | System.out.println("稍息");
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/command/StandEaseCommand.java:
--------------------------------------------------------------------------------
1 | package com.test.command;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/07
6 | * @Description: 稍息命令
7 | */
8 | public class StandEaseCommand implements Command {
9 | private Soldier soldier;
10 |
11 | public StandEaseCommand(Soldier soldier) {
12 | this.soldier = soldier;
13 | }
14 |
15 | @Override
16 | public void doCommand() {
17 | soldier.standAtEase();
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/common/ASUS.java:
--------------------------------------------------------------------------------
1 | package com.test.common;
2 |
3 | /**
4 | * @Author: zhouguaya
5 | * @Date: 2019/02/05
6 | * @Description: ASUS类
7 | */
8 | public class ASUS implements Computer {
9 | /**
10 | * 制造ASUS电脑
11 | */
12 | @Override
13 | public void make() {
14 | System.out.println("produce a ASUS Computer");
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/common/Computer.java:
--------------------------------------------------------------------------------
1 | package com.test.common;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/05
6 | * @Description: 电脑接口
7 | */
8 | public interface Computer {
9 | /**
10 | * 制造电脑的方法
11 | */
12 | void make();
13 | }
14 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/common/Lenovo.java:
--------------------------------------------------------------------------------
1 | package com.test.common;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/05
6 | * @Description: Lenovo电脑类
7 | */
8 | public class Lenovo implements Computer {
9 | /**
10 | * 制造Lenovo电脑
11 | */
12 | @Override
13 | public void make() {
14 | System.out.println("produce a Lenovo Computer");
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/common/MacBook.java:
--------------------------------------------------------------------------------
1 | package com.test.common;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/05
6 | * @Description: MacBook类
7 | */
8 | public class MacBook implements Computer {
9 | /**
10 | * 制造MacBook电脑
11 | */
12 | @Override
13 | public void make() {
14 | System.out.println("produce a MacBook Computer");
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/composite/Staff.java:
--------------------------------------------------------------------------------
1 | package com.test.composite;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | /**
7 | * @Author: zhouguanya
8 | * @Date: 2019/02/07
9 | * @Description: 员工类
10 | */
11 | public class Staff {
12 | private int id;
13 | private String name;
14 | private String dept;
15 | private List subordinateList;
16 | public Staff(int id, String name, String dept) {
17 | this.id = id;
18 | this.name = name;
19 | this.dept = dept;
20 | subordinateList = new ArrayList<>();
21 | }
22 |
23 | public void addSubordinate(Staff staff) {
24 | subordinateList.add(staff);
25 | }
26 |
27 | public List getSubordinates(){
28 | return subordinateList;
29 | }
30 |
31 | public String toString(){
32 | return ("Employee :[ id : "+ id
33 | +", name : "+ name + ", dept : "
34 | + dept+" ]");
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/criteria/AgeCriteria.java:
--------------------------------------------------------------------------------
1 | package com.test.criteria;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | /**
7 | * @Author: zhouguanya
8 | * @Date: 2019/02/06
9 | * @Description: 过滤年龄
10 | */
11 | public class AgeCriteria implements Criteria {
12 | @Override
13 | public List filter(List personList) {
14 | List filtered = new ArrayList<>();
15 | for (Person person : personList) {
16 | if (person.getAge() > 20) {
17 | filtered.add(person);
18 | }
19 | }
20 | return filtered;
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/criteria/Criteria.java:
--------------------------------------------------------------------------------
1 | package com.test.criteria;
2 |
3 | import java.util.List;
4 |
5 | /**
6 | * @Author: zhouguanya
7 | * @Date: 2019/02/06
8 | * @Description:
9 | */
10 | public interface Criteria {
11 | List filter(List personList);
12 | }
13 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/criteria/MaleCriteria.java:
--------------------------------------------------------------------------------
1 | package com.test.criteria;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | /**
7 | * @Author: zhouguanya
8 | * @Date: 2019/02/06
9 | * @Description: 过滤男性
10 | */
11 | public class MaleCriteria implements Criteria {
12 | @Override
13 | public List filter(List personList) {
14 | List filtered = new ArrayList<>();
15 | for (Person person : personList) {
16 | if ("Male".equals(person.getGender())) {
17 | filtered.add(person);
18 | }
19 | }
20 | return filtered;
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/criteria/Person.java:
--------------------------------------------------------------------------------
1 | package com.test.criteria;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/06
6 | * @Description:
7 | */
8 | public class Person {
9 | private String name;
10 | private int age;
11 | private String gender;
12 | public Person(String name, int age, String gender){
13 | this.name = name;
14 | this.age = age;
15 | this.gender = gender;
16 | }
17 | public String getName() {
18 | return name;
19 | }
20 | public int getAge() {
21 | return age;
22 | }
23 | public String getGender() {
24 | return gender;
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/decorator/ComputerDecorator.java:
--------------------------------------------------------------------------------
1 | package com.test.decorator;
2 |
3 | import com.test.common.Computer;
4 |
5 | /**
6 | * @Author: zhouguanya
7 | * @Date: 2019/02/07
8 | * @Description:
9 | */
10 | public abstract class ComputerDecorator implements Computer {
11 | Computer computer;
12 | public ComputerDecorator (Computer computer) {
13 | this.computer = computer;
14 | }
15 |
16 | public void make() {
17 | computer.make();
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/decorator/DecoratorPatternDemo.java:
--------------------------------------------------------------------------------
1 | package com.test.decorator;
2 |
3 | import com.test.common.ASUS;
4 | import com.test.common.Computer;
5 | import com.test.common.Lenovo;
6 | import com.test.common.MacBook;
7 |
8 | /**
9 | * @Author: zhouguanya
10 | * @Date: 2019/02/07
11 | * @Description: 装饰器模式测试类
12 | */
13 | public class DecoratorPatternDemo {
14 | public static void main(String[] args) {
15 | Computer whiteComputer = new WhiteComputerDecorator(new ASUS());
16 | whiteComputer.make();
17 | Computer goldenComputer = new GoldenComputerDecorator(new Lenovo());
18 | goldenComputer.make();
19 | Computer redComputer = new RedComputerDecorator(new MacBook());
20 | redComputer.make();
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/decorator/GoldenComputerDecorator.java:
--------------------------------------------------------------------------------
1 | package com.test.decorator;
2 |
3 | import com.test.common.Computer;
4 |
5 | /**
6 | * @Author: zhouguanya
7 | * @Date: 2019/02/07
8 | * @Description: 金色电脑装饰器
9 | */
10 | public class GoldenComputerDecorator extends ComputerDecorator {
11 | public GoldenComputerDecorator(Computer computer) {
12 | super(computer);
13 | }
14 |
15 | @Override
16 | public void make() {
17 | super.make();
18 | paintColor();
19 | }
20 |
21 | private void paintColor() {
22 | System.out.println("给电脑涂上金色");
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/decorator/RedComputerDecorator.java:
--------------------------------------------------------------------------------
1 | package com.test.decorator;
2 |
3 | import com.test.common.Computer;
4 |
5 | /**
6 | * @Author: zhouguanya
7 | * @Date: 2019/02/07
8 | * @Description: 红色电脑装饰器
9 | */
10 | public class RedComputerDecorator extends ComputerDecorator {
11 | public RedComputerDecorator(Computer computer) {
12 | super(computer);
13 | }
14 |
15 | @Override
16 | public void make() {
17 | super.make();
18 | paintColor();
19 | }
20 |
21 | private void paintColor() {
22 | System.out.println("给电脑涂上红色");
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/decorator/WhiteComputerDecorator.java:
--------------------------------------------------------------------------------
1 | package com.test.decorator;
2 |
3 | import com.test.common.Computer;
4 |
5 | /**
6 | * @Author: zhouguanya
7 | * @Date: 2019/02/07
8 | * @Description: 白色电脑装饰器
9 | */
10 | public class WhiteComputerDecorator extends ComputerDecorator {
11 | public WhiteComputerDecorator(Computer computer) {
12 | super(computer);
13 | }
14 |
15 | @Override
16 | public void make() {
17 | super.make();
18 | paintColor();
19 | }
20 |
21 | private void paintColor() {
22 | System.out.println("给电脑涂上白色");
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/facade/ComputerFacade.java:
--------------------------------------------------------------------------------
1 | package com.test.facade;
2 |
3 | import com.test.common.ASUS;
4 | import com.test.common.Computer;
5 | import com.test.common.Lenovo;
6 | import com.test.common.MacBook;
7 |
8 | /**
9 | * @Author: zhouguanya
10 | * @Date: 2019/02/07
11 | * @Description: 门面
12 | */
13 | public class ComputerFacade {
14 | private Computer lenovo;
15 | private Computer asus;
16 | private Computer macBook;
17 | public ComputerFacade() {
18 | lenovo = new Lenovo();
19 | asus = new ASUS();
20 | macBook = new MacBook();
21 | }
22 | public void makeLenovo(){
23 | lenovo.make();
24 | }
25 | public void makeASUS(){
26 | asus.make();
27 | }
28 | public void makeMacBook(){
29 | macBook.make();
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/facade/FacadePatternDemo.java:
--------------------------------------------------------------------------------
1 | package com.test.facade;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/07
6 | * @Description: 测试类
7 | */
8 | public class FacadePatternDemo {
9 | public static void main(String[] args) {
10 | ComputerFacade computerFacade = new ComputerFacade();
11 | computerFacade.makeLenovo();
12 | computerFacade.makeASUS();
13 | computerFacade.makeMacBook();
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/factorypattern/ComputerFactory.java:
--------------------------------------------------------------------------------
1 | package com.test.factorypattern;
2 |
3 | import com.test.common.ASUS;
4 | import com.test.common.Computer;
5 | import com.test.common.Lenovo;
6 | import com.test.common.MacBook;
7 |
8 | /**
9 | * @Author: zhouguanya
10 | * @Date: 2019/02/05
11 | * @Description: 电脑工厂
12 | */
13 | public class ComputerFactory {
14 | /**
15 | * createComputer方法返回不同品牌的电脑
16 | */
17 | public Computer createComputer(String type) {
18 | if (type == null || type.equals("")) {
19 | return null;
20 | }
21 | switch (type) {
22 | case "ASUS":
23 | return new ASUS();
24 | case "Lenovo":
25 | return new Lenovo();
26 | case "MacBook":
27 | return new MacBook();
28 | default:
29 | return null;
30 | }
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/factorypattern/ComputerFactoryDemo.java:
--------------------------------------------------------------------------------
1 | package com.test.factorypattern;
2 |
3 | import com.test.common.Computer;
4 |
5 | /**
6 | * @Author: zhouguanya
7 | * @Date: 2019/02/05
8 | * @Description: 测试类
9 | */
10 | public class ComputerFactoryDemo {
11 | public static void main(String[] args) {
12 | ComputerFactory computerFactory = new ComputerFactory();
13 | //创建ASUS
14 | Computer asus = computerFactory.createComputer("ASUS");
15 | asus.make();
16 | //创建Lenovo
17 | Computer lenovo = computerFactory.createComputer("Lenovo");
18 | lenovo.make();
19 | //创建MacBook
20 | Computer macBook = computerFactory.createComputer("MacBook");
21 | macBook.make();
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/flyweight/Computer.java:
--------------------------------------------------------------------------------
1 | package com.test.flyweight;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/05
6 | * @Description: 电脑接口
7 | */
8 | public interface Computer {
9 | /**
10 | * 制造电脑的方法
11 | */
12 | void make();
13 | }
14 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/flyweight/ComputerFactory.java:
--------------------------------------------------------------------------------
1 | package com.test.flyweight;
2 |
3 | import java.util.HashMap;
4 | import java.util.Map;
5 |
6 | /**
7 | * @Author: zhouguanya
8 | * @Date: 2019/02/07
9 | * @Description: 电脑工厂
10 | */
11 | public class ComputerFactory {
12 | private static final Map computerMap = new HashMap<>();
13 |
14 | public static Computer getComputer(String brand) {
15 | Computer computer = computerMap.get(brand);
16 | if (computer == null) {
17 | computer = new ComputerProducer(brand);
18 | computerMap.put(brand, computer);
19 |
20 | }
21 | return computer;
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/flyweight/ComputerProducer.java:
--------------------------------------------------------------------------------
1 | package com.test.flyweight;
2 |
3 | /**
4 | * @Author: zhouguaya
5 | * @Date: 2019/02/05
6 | * @Description: 电脑制造类
7 | */
8 | public class ComputerProducer implements Computer {
9 |
10 | private String brand;
11 |
12 | public ComputerProducer(String brand) {
13 | this.brand = brand;
14 | }
15 |
16 | /**
17 | * 制造电脑
18 | */
19 | @Override
20 | public void make() {
21 | System.out.println("produce a " + brand + " Computer");
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/flyweight/FlyweightPatternDemo.java:
--------------------------------------------------------------------------------
1 | package com.test.flyweight;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/07
6 | * @Description: 享原模式测试类
7 | */
8 | public class FlyweightPatternDemo {
9 | static String brands[] = { "ASUA", "Lenovo", "MacBook"};
10 |
11 | public static void main(String[] args) {
12 |
13 | for (int i = 0; i < 20; i++) {
14 | Computer computer = ComputerFactory.getComputer(getRandomBrand());
15 | computer.make();
16 | }
17 | }
18 |
19 | private static String getRandomBrand() {
20 | return brands[(int)(Math.random() * brands.length)];
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/interceptingfilter/Client.java:
--------------------------------------------------------------------------------
1 | package com.test.interceptingfilter;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/08
6 | * @Description: 客户端
7 | */
8 | public class Client {
9 | FilterManager filterManager;
10 |
11 | public void setFilterManager(FilterManager filterManager){
12 | this.filterManager = filterManager;
13 | }
14 |
15 | public void sendRequest(){
16 | filterManager.filterRequest();
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/interceptingfilter/DirtyFilter.java:
--------------------------------------------------------------------------------
1 | package com.test.interceptingfilter;
2 |
3 |
4 | /**
5 | * @Author: zhouguanya
6 | * @Date: 2019/02/08
7 | * @Description: 脏话过滤器
8 | */
9 | public class DirtyFilter implements Filter {
10 | /**
11 | * 过滤方法
12 | */
13 | @Override
14 | public void doFilter() {
15 | System.out.println("Execute Dirty Words Filter");
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/interceptingfilter/Filter.java:
--------------------------------------------------------------------------------
1 | package com.test.interceptingfilter;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/08
6 | * @Description: 过滤器
7 | */
8 | public interface Filter {
9 | /**
10 | * 过滤方法
11 | */
12 | void doFilter();
13 | }
14 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/interceptingfilter/FilterChain.java:
--------------------------------------------------------------------------------
1 | package com.test.interceptingfilter;
2 |
3 |
4 | import java.util.ArrayList;
5 | import java.util.List;
6 |
7 | /**
8 | * @Author: zhouguanya
9 | * @Date: 2019/02/08
10 | * @Description: 过滤器链
11 | */
12 | public class FilterChain {
13 | //过滤器集合
14 | List filterList = new ArrayList<>();
15 | private Target target;
16 |
17 | /**
18 | * 添加过滤器
19 | */
20 | public void addFilter(Filter filter){
21 | filterList.add(filter);
22 | }
23 |
24 | /**
25 | * 执行过滤器
26 | */
27 | public void execute(){
28 | //前置拦截
29 | for (Filter filter : filterList) {
30 | filter.doFilter();
31 | }
32 | //执行目标对象
33 | target.execute();
34 | }
35 |
36 | /**
37 | * 设置目标对象
38 | */
39 | public void setTarget(Target target){
40 | this.target = target;
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/interceptingfilter/FilterManager.java:
--------------------------------------------------------------------------------
1 | package com.test.interceptingfilter;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/08
6 | * @Description: 过滤器管理员
7 | */
8 | public class FilterManager {
9 | private FilterChain filterChain;
10 |
11 | public FilterManager(Target target){
12 | filterChain = new FilterChain();
13 | filterChain.setTarget(target);
14 | }
15 | public void setFilter(Filter filter){
16 | filterChain.addFilter(filter);
17 | }
18 |
19 | public void filterRequest(){
20 | filterChain.execute();
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/interceptingfilter/InterceptingFilterDemo.java:
--------------------------------------------------------------------------------
1 | package com.test.interceptingfilter;
2 |
3 |
4 | /**
5 | * @Author: zhouguanya
6 | * @Date: 2019/02/08
7 | * @Description: 拦截过滤器模式测试类
8 | */
9 | public class InterceptingFilterDemo {
10 | public static void main(String[] args) {
11 |
12 | FilterManager filterManager = new FilterManager(new Target());
13 | //装配各种过滤器
14 | filterManager.setFilter(new DirtyFilter());
15 | filterManager.setFilter(new PrivateFilter());
16 | filterManager.setFilter(new SensitiveFilter());
17 |
18 | Client client = new Client();
19 | client.setFilterManager(filterManager);
20 | client.sendRequest();
21 | }
22 | }
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/interceptingfilter/PrivateFilter.java:
--------------------------------------------------------------------------------
1 | package com.test.interceptingfilter;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/08
6 | * @Description: 隐私过滤器
7 | */
8 | public class PrivateFilter implements Filter {
9 | /**
10 | * 过滤方法
11 | */
12 | @Override
13 | public void doFilter() {
14 | System.out.println("Execute Private Words Filter");
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/interceptingfilter/SensitiveFilter.java:
--------------------------------------------------------------------------------
1 | package com.test.interceptingfilter;
2 |
3 |
4 | /**
5 | * @Author: zhouguanya
6 | * @Date: 2019/02/08
7 | * @Description: 敏感词过滤器
8 | */
9 | public class SensitiveFilter implements Filter {
10 | /**
11 | * 过滤方法
12 | */
13 | @Override
14 | public void doFilter() {
15 | System.out.println("Execute Sensitive Words Filter");
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/interceptingfilter/Target.java:
--------------------------------------------------------------------------------
1 | package com.test.interceptingfilter;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/08
6 | * @Description: 目标对象
7 | */
8 | public class Target {
9 | public void execute(){
10 | System.out.println("The Final Target Object");
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/interpreter/AndExpression.java:
--------------------------------------------------------------------------------
1 | package com.test.interpreter;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/07
6 | * @Description: 与表达式
7 | */
8 | public class AndExpression implements Expression {
9 | private Expression expression1 = null;
10 | private Expression expression2 = null;
11 | public AndExpression(Expression expression1, Expression expression2) {
12 | this.expression1 = expression1;
13 | this.expression2 = expression2;
14 | }
15 |
16 | @Override
17 | public boolean interpret() {
18 | return expression1.interpret() && expression2.interpret();
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/interpreter/Expression.java:
--------------------------------------------------------------------------------
1 | package com.test.interpreter;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/07
6 | * @Description: 解释器接口
7 | */
8 | public interface Expression {
9 | boolean interpret();
10 | }
11 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/interpreter/InterpreterPatternDemo.java:
--------------------------------------------------------------------------------
1 | package com.test.interpreter;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/07
6 | * @Description: 解释器模式测试类
7 | */
8 | public class InterpreterPatternDemo {
9 | public static void main(String[] args) {
10 | NumericalExpression expression1
11 | = new NumericalExpression(10, 8);
12 | NumericalExpression expression2
13 | = new NumericalExpression(10, 20);
14 | AndExpression andExpression
15 | = new AndExpression(expression1, expression2);
16 | OrExpression orExpression
17 | = new OrExpression(expression1, expression2);
18 | System.out.println("10 > 8 && 10 > 20 ? " + andExpression.interpret());
19 | System.out.println("10 > 8 || 10 > 20 ? " + orExpression.interpret());
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/interpreter/NumericalExpression.java:
--------------------------------------------------------------------------------
1 | package com.test.interpreter;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/07
6 | * @Description:
7 | */
8 | public class NumericalExpression implements Expression {
9 | private int value1;
10 | private int value2;
11 |
12 | public NumericalExpression(int value1, int value2) {
13 | this.value1 = value1;
14 | this.value2 = value2;
15 | }
16 |
17 | @Override
18 | public boolean interpret() {
19 | return (value1 - value2) > 0;
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/interpreter/OrExpression.java:
--------------------------------------------------------------------------------
1 | package com.test.interpreter;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/07
6 | * @Description: 或表达式解释器
7 | */
8 | public class OrExpression implements Expression {
9 | private Expression expression1 = null;
10 | private Expression expression2 = null;
11 |
12 | public OrExpression(Expression expression1, Expression expression2) {
13 | this.expression1 = expression1;
14 | this.expression2 = expression2;
15 | }
16 |
17 | @Override
18 | public boolean interpret() {
19 | return expression1.interpret() || expression2.interpret();
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/iterator/Container.java:
--------------------------------------------------------------------------------
1 | package com.test.iterator;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/07
6 | * @Description:
7 | */
8 | public interface Container {
9 | Iterator getIterator();
10 | }
11 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/iterator/IntegerIterator.java:
--------------------------------------------------------------------------------
1 | package com.test.iterator;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/07
6 | * @Description:
7 | */
8 | public class IntegerIterator implements Iterator {
9 | private Integer[] numbers;
10 | int index;
11 | public IntegerIterator (Integer[] numbers) {
12 | this.numbers = numbers;
13 | }
14 |
15 | @Override
16 | public boolean hasNext() {
17 | if (index < numbers.length) {
18 | return true;
19 | }
20 | return false;
21 | }
22 |
23 | @Override
24 | public Integer next() {
25 | if (hasNext()) {
26 | return numbers[index++];
27 | }
28 | return null;
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/iterator/IntegerRepository.java:
--------------------------------------------------------------------------------
1 | package com.test.iterator;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/07
6 | * @Description:
7 | */
8 | public class IntegerRepository implements Container {
9 | private Integer[] numbers;
10 |
11 | public IntegerRepository (Integer[] numbers) {
12 | this.numbers = numbers;
13 | }
14 |
15 | @Override
16 | public Iterator getIterator() {
17 | return new IntegerIterator(numbers);
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/iterator/Iterator.java:
--------------------------------------------------------------------------------
1 | package com.test.iterator;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/07
6 | * @Description: 迭代器接口
7 | */
8 | public interface Iterator {
9 | boolean hasNext();
10 | T next();
11 | }
12 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/iterator/IteratorPatternDemo.java:
--------------------------------------------------------------------------------
1 | package com.test.iterator;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/07
6 | * @Description: 迭代器模式测试类
7 | */
8 | public class IteratorPatternDemo {
9 | public static void main(String[] args) {
10 | Integer numbers[] = {1, 2, 3, 4, 5};
11 | IntegerRepository repository = new IntegerRepository(numbers);
12 | Iterator iterator = repository.getIterator();
13 | while (iterator.hasNext()) {
14 | System.out.println(iterator.next());
15 | }
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/mediator/BBS.java:
--------------------------------------------------------------------------------
1 | package com.test.mediator;
2 |
3 | import java.util.Date;
4 |
5 | /**
6 | * @Author: zhouguanya
7 | * @Date: 2019/02/07
8 | * @Description: 论坛
9 | */
10 | public class BBS {
11 | public static void showMessage(User user, String message){
12 | System.out.println(new Date().toString()
13 | + " [" + user.getName() +"] : " + message);
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/mediator/MediatorPatternDemo.java:
--------------------------------------------------------------------------------
1 | package com.test.mediator;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/07
6 | * @Description: 中介者模式测试类
7 | */
8 | public class MediatorPatternDemo {
9 | public static void main(String[] args) {
10 | User robert = new User("Tom");
11 | User john = new User("John");
12 | robert.sendMessage("Headline : celestial dog devouring the sun !");
13 | john.sendMessage("Reply : no, it is Solar Eclipse ~");
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/mediator/User.java:
--------------------------------------------------------------------------------
1 | package com.test.mediator;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/07
6 | * @Description: 用户
7 | */
8 | public class User {
9 | private String name;
10 |
11 | public String getName() {
12 | return name;
13 | }
14 |
15 | public User(String name){
16 | this.name = name;
17 | }
18 |
19 | public void sendMessage(String message){
20 | BBS.showMessage(this,message);
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/memento/Memento.java:
--------------------------------------------------------------------------------
1 | package com.test.memento;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/07
6 | * @Description:
7 | */
8 | public class Memento {
9 | private String state;
10 | public Memento(String state){
11 | this.state = state;
12 | }
13 | public String getState(){
14 | return state;
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/memento/MementoPatternDemo.java:
--------------------------------------------------------------------------------
1 | package com.test.memento;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/07
6 | * @Description: 备忘录模式测试类
7 | */
8 | public class MementoPatternDemo {
9 | public static void main(String[] args) {
10 | Original original = new Original();
11 | StateKeeper keeper = new StateKeeper();
12 | original.setState("State 1");
13 | keeper.addState(original.saveMementoState());
14 | //状态变更
15 | original.setState("State 2");
16 | keeper.addState(original.saveMementoState());
17 | //状态变更
18 | original.setState("State 3");
19 | System.out.println("Current State is :" + original.getState());
20 | //第1次保存的状态
21 | original.getMementoState(keeper.get(0));
22 | System.out.println("Initial State: " + original.getState());
23 | //第2次保存的状态
24 | original.getMementoState(keeper.get(1));
25 | System.out.println("Second State: " + original.getState());
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/memento/Original.java:
--------------------------------------------------------------------------------
1 | package com.test.memento;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/07
6 | * @Description: 原始类
7 | */
8 | public class Original {
9 | private String state;
10 |
11 | public String getState() {
12 | return state;
13 | }
14 |
15 | public void setState(String state) {
16 | this.state = state;
17 | }
18 |
19 | public Memento saveMementoState() {
20 | return new Memento(state);
21 | }
22 |
23 | public void getMementoState(Memento memento) {
24 | state = memento.getState();
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/memento/StateKeeper.java:
--------------------------------------------------------------------------------
1 | package com.test.memento;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | /**
7 | * @Author: zhouguanya
8 | * @Date: 2019/02/07
9 | * @Description: 状态管理类
10 | */
11 | public class StateKeeper {
12 | private List mementoList = new ArrayList<>();
13 |
14 | public void addState(Memento state){
15 | mementoList.add(state);
16 | }
17 |
18 | public Memento get(int index){
19 | return mementoList.get(index);
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/nullobject/AbstractObject.java:
--------------------------------------------------------------------------------
1 | package com.test.nullobject;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/08
6 | * @Description: 空对象接口
7 | */
8 | public interface AbstractObject {
9 | boolean isNull();
10 | void show();
11 | }
12 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/nullobject/NullObject.java:
--------------------------------------------------------------------------------
1 | package com.test.nullobject;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/08
6 | * @Description:
7 | */
8 | public class NullObject implements AbstractObject {
9 | private String name;
10 | public NullObject(String name) {
11 | this.name = name;
12 | }
13 | @Override
14 | public boolean isNull() {
15 | return true;
16 | }
17 |
18 | @Override
19 | public void show() {
20 | // do nothing
21 | System.out.println(name + " Object not exist");
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/nullobject/NullPatternDemo.java:
--------------------------------------------------------------------------------
1 | package com.test.nullobject;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/08
6 | * @Description: 空对象模式实现类
7 | */
8 | public class NullPatternDemo {
9 | public static void main(String[] args) {
10 | AbstractObject object1 = ObjectFactory.getObject("light");
11 | AbstractObject object2 = ObjectFactory.getObject("bed");
12 | AbstractObject object3 = ObjectFactory.getObject("table");
13 | AbstractObject object4 = ObjectFactory.getObject("sun");
14 | object1.show();
15 | object2.show();
16 | object3.show();
17 | object4.show();
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/nullobject/ObjectFactory.java:
--------------------------------------------------------------------------------
1 | package com.test.nullobject;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/08
6 | * @Description: 对象工厂
7 | */
8 | public class ObjectFactory {
9 | public static final String[] names = {"table", "light", "bed"};
10 | public static AbstractObject getObject(String name){
11 | for (int i = 0; i < names.length; i++) {
12 | if (names[i].equalsIgnoreCase(name)){
13 | return new RealObject(name);
14 | } else {
15 | return new NullObject(name);
16 | }
17 | }
18 | return new NullObject("");
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/nullobject/RealObject.java:
--------------------------------------------------------------------------------
1 | package com.test.nullobject;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/08
6 | * @Description:
7 | */
8 | public class RealObject implements AbstractObject {
9 | private String name;
10 | public RealObject(String name) {
11 | this.name = name;
12 | }
13 | @Override
14 | public boolean isNull() {
15 | return false;
16 | }
17 |
18 | @Override
19 | public void show() {
20 | System.out.println("real object " + name + " shows now");
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/observer/Observer.java:
--------------------------------------------------------------------------------
1 | package com.test.observer;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/08
6 | * @Description: 观察接口
7 | */
8 | public interface Observer {
9 | void listen(String message);
10 | }
11 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/observer/ObserverPatternDemo.java:
--------------------------------------------------------------------------------
1 | package com.test.observer;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/08
6 | * @Description: 观察者模式测试类
7 | */
8 | public class ObserverPatternDemo {
9 | public static void main(String[] args) {
10 | WeChatPulic weChatPulic = new WeChatPulic();
11 | WeChatUser weChatUser1 = new WeChatUser("Jack");
12 | WeChatUser weChatUser2 = new WeChatUser("Tom");
13 | WeChatUser weChatUser3 = new WeChatUser("John");
14 | weChatPulic.addObserver(weChatUser1);
15 | weChatPulic.addObserver(weChatUser2);
16 | weChatPulic.addObserver(weChatUser3);
17 | weChatPulic.setMessage("Hello World");
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/observer/WeChatPulic.java:
--------------------------------------------------------------------------------
1 | package com.test.observer;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | /**
7 | * @Author: zhouguanya
8 | * @Date: 2019/02/08
9 | * @Description: 微信公众号
10 | */
11 | public class WeChatPulic {
12 | //观察者列表
13 | private List observerList;
14 |
15 | private String message;
16 |
17 | public WeChatPulic() {
18 | observerList = new ArrayList<>();
19 | }
20 | // 状态变更通知观察者
21 | public void setMessage(String message) {
22 | this.message = message;
23 | System.out.println("微信公众号更新消息:" + message);
24 | notifyObserverList();
25 | }
26 |
27 | public void addObserver(Observer observer) {
28 | observerList.add(observer);
29 | }
30 |
31 | public void notifyObserverList() {
32 | for (Observer observer : observerList) {
33 | observer.listen(message);
34 | }
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/observer/WeChatUser.java:
--------------------------------------------------------------------------------
1 | package com.test.observer;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/08
6 | * @Description: 微信用户
7 | */
8 | public class WeChatUser implements Observer {
9 | private String name;
10 | private String message;
11 | public WeChatUser(String name) {
12 | this.name = name;
13 | }
14 | @Override
15 | public void listen(String message) {
16 | this.message = message;
17 | System.out.printf("%s收到微信公众号的消息:%s%n", name, this.message);
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/prototype/ASUS.java:
--------------------------------------------------------------------------------
1 | package com.test.prototype;
2 |
3 | /**
4 | * @Author: zhouguaya
5 | * @Date: 2019/02/05
6 | * @Description: ASUS类
7 | */
8 | public class ASUS extends Computer {
9 | /**
10 | * 制造ASUS电脑
11 | */
12 | @Override
13 | public void make() {
14 | System.out.println("produce a ASUS Computer");
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/prototype/Computer.java:
--------------------------------------------------------------------------------
1 | package com.test.prototype;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/05
6 | * @Description: 电脑接口
7 | */
8 | public abstract class Computer implements Cloneable{
9 | protected String type;
10 | /**
11 | * 制造电脑的方法
12 | */
13 | void make() {
14 |
15 | }
16 |
17 | /**
18 | * 克隆方法
19 | */
20 | public Object clone() {
21 | Object clone = null;
22 | try {
23 | clone = super.clone();
24 | } catch (CloneNotSupportedException e) {
25 | e.printStackTrace();
26 | }
27 | return clone;
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/prototype/ComputerPrototype.java:
--------------------------------------------------------------------------------
1 | package com.test.prototype;
2 |
3 | import java.util.HashMap;
4 | import java.util.Map;
5 |
6 | /**
7 | * @Author: zhouguanya
8 | * @Date: 2019/02/06
9 | * @Description: 电脑原型类
10 | */
11 | public class ComputerPrototype {
12 | private static Map computerMap = new HashMap<>();
13 | static {
14 | computerMap.put("ASUS", new ASUS());
15 | computerMap.put("Lenovo", new Lenovo());
16 | computerMap.put("MacBook", new MacBook());
17 | }
18 |
19 | public static Computer getComputer(String type) {
20 | Computer computer = computerMap.get(type);
21 | if (computer != null) {
22 | //返回克隆对象
23 | return (Computer) computer.clone();
24 | }
25 | return null;
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/prototype/Lenovo.java:
--------------------------------------------------------------------------------
1 | package com.test.prototype;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/05
6 | * @Description: Lenovo电脑类
7 | */
8 | public class Lenovo extends Computer {
9 | /**
10 | * 制造Lenovo电脑
11 | */
12 | @Override
13 | public void make() {
14 | System.out.println("produce a Lenovo Computer");
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/prototype/MacBook.java:
--------------------------------------------------------------------------------
1 | package com.test.prototype;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/05
6 | * @Description: MacBook
7 | */
8 | public class MacBook extends Computer {
9 | /**
10 | * 制造MacBook电脑
11 | */
12 | @Override
13 | public void make() {
14 | System.out.println("produce a MacBook Computer");
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/prototype/PrototypePatternDemo.java:
--------------------------------------------------------------------------------
1 | package com.test.prototype;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/06
6 | * @Description: 原型模式测试
7 | */
8 | public class PrototypePatternDemo {
9 | public static void main(String[] args) {
10 | Computer asus = ComputerPrototype.getComputer("ASUS");
11 | asus.make();
12 | Computer lenovo = ComputerPrototype.getComputer("Lenovo");
13 | lenovo.make();
14 | Computer macBook = ComputerPrototype.getComputer("MacBook");
15 | macBook.make();
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/proxy/BuyHouse.java:
--------------------------------------------------------------------------------
1 | package com.test.proxy;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/07
6 | * @Description: 买房接口
7 | */
8 | public interface BuyHouse {
9 | void buy();
10 | }
11 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/proxy/Customer.java:
--------------------------------------------------------------------------------
1 | package com.test.proxy;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/07
6 | * @Description: 客户
7 | */
8 | public class Customer implements BuyHouse {
9 | @Override
10 | public void buy() {
11 | System.out.println("我是客户,我想买房");
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/proxy/CustomerProxy.java:
--------------------------------------------------------------------------------
1 | package com.test.proxy;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/07
6 | * @Description: 客户代理
7 | */
8 | public class CustomerProxy implements BuyHouse {
9 | private Customer customer;
10 |
11 | public CustomerProxy(Customer customer) {
12 | this.customer = customer;
13 | }
14 |
15 | @Override
16 | public void buy() {
17 | customer.buy();
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/proxy/ProxyPatternDemo.java:
--------------------------------------------------------------------------------
1 | package com.test.proxy;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/07
6 | * @Description: 代理模式测试类
7 | */
8 | public class ProxyPatternDemo {
9 | public static void main(String[] args) {
10 | CustomerProxy customerProxy = new CustomerProxy(new Customer());
11 | customerProxy.buy();
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/singleton/SingletonObject.java:
--------------------------------------------------------------------------------
1 | package com.test.singleton;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/05
6 | * @Description: 单例
7 | */
8 | public class SingletonObject {
9 | //创建 SingletonObject 的一个对象
10 | private static SingletonObject instance = new SingletonObject();
11 | //让构造函数为 private,这样该类就不会被实例化
12 | private SingletonObject(){
13 |
14 | }
15 | //获取唯一可用的对象
16 | public static SingletonObject getInstance(){
17 | return instance;
18 | }
19 | public void showMessage(){
20 | System.out.println("Hello World!");
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/singleton/SingletonObjectDemo.java:
--------------------------------------------------------------------------------
1 | package com.test.singleton;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/05
6 | * @Description: 单例测试类
7 | */
8 | public class SingletonObjectDemo {
9 | public static void main(String[] args) {
10 | SingletonObject singletonObject = SingletonObject.getInstance();
11 | singletonObject.showMessage();
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/state/Rain.java:
--------------------------------------------------------------------------------
1 | package com.test.state;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/08
6 | * @Description: 下雨
7 | */
8 | public class Rain implements State {
9 | @Override
10 | public String getState() {
11 | return "今天的天气:下雨";
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/state/State.java:
--------------------------------------------------------------------------------
1 | package com.test.state;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/08
6 | * @Description: 状态
7 | */
8 | public interface State {
9 | //获取天气情况
10 | String getState();
11 | }
12 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/state/StatePatternDemo.java:
--------------------------------------------------------------------------------
1 | package com.test.state;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/08
6 | * @Description: 状态模式测试类
7 | */
8 | public class StatePatternDemo {
9 | public static void main(String[] args) {
10 | Weather weather = new Weather();
11 | weather.setState(new Rain());
12 | System.out.println(weather.getWeather());
13 | weather.setState(new Sunshine());
14 | System.out.println(weather.getWeather());
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/state/Sunshine.java:
--------------------------------------------------------------------------------
1 | package com.test.state;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/08
6 | * @Description: 晴天
7 | */
8 | public class Sunshine implements State {
9 | @Override
10 | public String getState() {
11 | return "今天的天气:晴天";
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/state/Weather.java:
--------------------------------------------------------------------------------
1 | package com.test.state;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/08
6 | * @Description: 天气类
7 | */
8 | public class Weather {
9 | private State state;
10 |
11 | public void setState(State state) {
12 | this.state = state;
13 | }
14 | public String getWeather() {
15 | return state.getState();
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/strategy/Addition.java:
--------------------------------------------------------------------------------
1 | package com.test.strategy;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/08
6 | * @Description: 加法
7 | */
8 | public class Addition implements Strategy {
9 |
10 | @Override
11 | public int execute(int num1, int num2) {
12 | return num1 + num2;
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/strategy/Context.java:
--------------------------------------------------------------------------------
1 | package com.test.strategy;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/08
6 | * @Description:
7 | */
8 | public class Context {
9 | private Strategy strategy;
10 | public Context(Strategy strategy) {
11 | this.strategy = strategy;
12 | }
13 |
14 | public int execute(int num1, int num2) {
15 | return strategy.execute(num1, num2);
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/strategy/Multiplication.java:
--------------------------------------------------------------------------------
1 | package com.test.strategy;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/08
6 | * @Description:
7 | */
8 | public class Multiplication implements Strategy {
9 | @Override
10 | public int execute(int num1, int num2) {
11 | return num1 * num2;
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/strategy/Strategy.java:
--------------------------------------------------------------------------------
1 | package com.test.strategy;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/08
6 | * @Description: 策略类
7 | */
8 | public interface Strategy {
9 | int execute(int num1, int num2);
10 | }
11 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/strategy/StrategyPatternDemo.java:
--------------------------------------------------------------------------------
1 | package com.test.strategy;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/08
6 | * @Description: 策略模式测试类
7 | */
8 | public class StrategyPatternDemo {
9 | public static void main(String[] args) {
10 | Context context = new Context(new Addition());
11 | System.out.println("10 + 20 = " + context.execute(10, 20));
12 | context = new Context(new Subtraction());
13 | System.out.println("10 - 20 = " + context.execute(10, 20));
14 | context = new Context(new Multiplication());
15 | System.out.println("10 * 20 = " + context.execute(10, 20));
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/strategy/Subtraction.java:
--------------------------------------------------------------------------------
1 | package com.test.strategy;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/08
6 | * @Description: 减法
7 | */
8 | public class Subtraction implements Strategy {
9 | @Override
10 | public int execute(int num1, int num2) {
11 | return num1 - num2;
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/template/Game.java:
--------------------------------------------------------------------------------
1 | package com.test.template;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/08
6 | * @Description: 游戏类
7 | */
8 | public abstract class Game {
9 | abstract void init();
10 | abstract void start();
11 | abstract void pause();
12 | abstract void end();
13 |
14 | void play() {
15 | init();
16 | start();
17 | pause();
18 | end();
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/template/SuperMarie.java:
--------------------------------------------------------------------------------
1 | package com.test.template;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/08
6 | * @Description: 超级玛丽
7 | */
8 | public class SuperMarie extends Game {
9 | @Override
10 | void init() {
11 | System.out.println("Init SuperMarie Game");
12 | }
13 |
14 | @Override
15 | void start() {
16 | System.out.println("Start SuperMarie Game");
17 | }
18 |
19 | @Override
20 | void pause() {
21 | System.out.println("Pause SuperMarie Game");
22 | }
23 |
24 | @Override
25 | void end() {
26 | System.out.println("End SuperMarie Game");
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/template/TemplatePatternDemo.java:
--------------------------------------------------------------------------------
1 | package com.test.template;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/08
6 | * @Description: 模板模式测试类
7 | */
8 | public class TemplatePatternDemo {
9 | public static void main(String[] args) {
10 | Game tetris = new Tetris();
11 | tetris.play();
12 | System.out.println("-----------分割线-----------");
13 | Game superMarie = new SuperMarie();
14 | superMarie.play();
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/chapter20/src/main/java/com/test/template/Tetris.java:
--------------------------------------------------------------------------------
1 | package com.test.template;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2019/02/08
6 | * @Description: 俄罗斯方块
7 | */
8 | public class Tetris extends Game {
9 | @Override
10 | void init() {
11 | System.out.println("Init Tetris Game");
12 | }
13 |
14 | @Override
15 | void start() {
16 | System.out.println("Start Tetris Game");
17 | }
18 |
19 | @Override
20 | void pause() {
21 | System.out.println("Pause Tetris Game");
22 | }
23 |
24 | @Override
25 | void end() {
26 | System.out.println("End Tetris Game");
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/chapter3/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | project.demo
7 | com.test.spring5
8 | 1.0-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | chapter3
13 |
14 |
15 |
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/aop/cglib/annotation/CgLibAnnotationDemo.java:
--------------------------------------------------------------------------------
1 | package com.test.aop.cglib.annotation;
2 |
3 | import org.springframework.context.ApplicationContext;
4 | import org.springframework.context.support.ClassPathXmlApplicationContext;
5 |
6 | /**
7 | * @Author zhouguanya
8 | * @Date 2018/8/27
9 | * @Description cglib aop测试
10 | */
11 | public class CgLibAnnotationDemo {
12 | public static void main(String[] args) throws InterruptedException {
13 | ApplicationContext applicationContext
14 | = new ClassPathXmlApplicationContext("classpath:spring-chapter3-annotationcglibaop.xml");
15 | Desk desk = (Desk) applicationContext.getBean("desk");
16 | Table table = (Table) applicationContext.getBean("table");
17 | desk.location();
18 | System.out.println("-----分割线-----");
19 | Thread.sleep(10000);
20 | table.location();
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/aop/cglib/annotation/Desk.java:
--------------------------------------------------------------------------------
1 | package com.test.aop.cglib.annotation;
2 |
3 | import org.springframework.stereotype.Component;
4 |
5 | /**
6 | * @Author zhouguanya
7 | * @Date 2018/8/27
8 | * @Description 课桌
9 | */
10 | @Component
11 | public class Desk {
12 | /**
13 | * 打印位置信息
14 | */
15 | public void location () throws InterruptedException {
16 | //模拟耗时,方便观察输出结果
17 | Thread.sleep(10000);
18 | System.out.println("我是课桌,我被放在教室中");
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/aop/cglib/annotation/Table.java:
--------------------------------------------------------------------------------
1 | package com.test.aop.cglib.annotation;
2 |
3 | import org.springframework.stereotype.Component;
4 |
5 | /**
6 | * @Author zhouguanya
7 | * @Date 2018/8/27
8 | * @Description 桌子
9 | */
10 | @Component
11 | public class Table {
12 | /**
13 | * 打印位置信息
14 | */
15 | public void location() throws InterruptedException {
16 | //模拟耗时,方便观察输出结果
17 | Thread.sleep(10000);
18 | System.out.println("我是餐桌,我被放在厨房中");
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/aop/cglib/xml/CgLibXmlDemo.java:
--------------------------------------------------------------------------------
1 | package com.test.aop.cglib.xml;
2 |
3 | import org.springframework.context.ApplicationContext;
4 | import org.springframework.context.support.ClassPathXmlApplicationContext;
5 |
6 | /**
7 | * @Author zhouguanya
8 | * @Date 2018/8/27
9 | * @Description cglib aop测试
10 | */
11 | public class CgLibXmlDemo {
12 | public static void main(String[] args) throws InterruptedException {
13 | ApplicationContext applicationContext
14 | = new ClassPathXmlApplicationContext("classpath:spring-chapter3-xmlcglib.xml");
15 | Desk desk = (Desk) applicationContext.getBean("desk");
16 | Table table = (Table) applicationContext.getBean("table");
17 | desk.location();
18 | System.out.println("-----分割线-----");
19 | Thread.sleep(10000);
20 | table.location();
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/aop/cglib/xml/CglibXmlAspect.java:
--------------------------------------------------------------------------------
1 | package com.test.aop.cglib.xml;
2 |
3 | import java.text.SimpleDateFormat;
4 | import java.util.Date;
5 |
6 | /**
7 | * @Author: zhouguanya
8 | * @Date: 2018/8/27 19:500
9 | * @Description: 横切关注点,打印开始和结束的时间
10 | */
11 | public class CglibXmlAspect {
12 |
13 | /**
14 | * 打印事件开始的时间
15 | */
16 | public void startDate() {
17 | SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
18 | String startEatDate = simpleDateFormat.format(new Date());
19 | System.out.println("开始的时间是:" + startEatDate);
20 | }
21 |
22 | /**
23 | * 打印事件结束的时间
24 | */
25 | public void endDate() {
26 | SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
27 | String endEatDate = simpleDateFormat.format(new Date());
28 | System.out.println("结束的时间是:" + endEatDate);
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/aop/cglib/xml/Desk.java:
--------------------------------------------------------------------------------
1 | package com.test.aop.cglib.xml;
2 |
3 | /**
4 | * @Author zhouguanya
5 | * @Date 2018/8/27
6 | * @Description 课桌
7 | */
8 | public class Desk {
9 | /**
10 | * 打印位置信息
11 | */
12 | public void location () throws InterruptedException {
13 | //模拟耗时,方便观察输出结果
14 | Thread.sleep(10000);
15 | System.out.println("我是课桌,我被放在教室中");
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/aop/cglib/xml/Table.java:
--------------------------------------------------------------------------------
1 | package com.test.aop.cglib.xml;
2 |
3 | /**
4 | * @Author zhouguanya
5 | * @Date 2018/8/27
6 | * @Description 桌子
7 | */
8 | public class Table {
9 | /**
10 | * 打印位置信息
11 | */
12 | public void location() throws InterruptedException {
13 | //模拟耗时,方便观察输出结果
14 | Thread.sleep(10000);
15 | System.out.println("我是餐桌,我被放在厨房中");
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/aop/jdk/annotation/SpringAopAnnotationDemo.java:
--------------------------------------------------------------------------------
1 | package com.test.aop.jdk.annotation;
2 |
3 | import com.test.aop.jdk.xml.Fruit;
4 | import org.springframework.context.ApplicationContext;
5 | import org.springframework.context.support.ClassPathXmlApplicationContext;
6 |
7 | /**
8 | * @Author: zhouguanya
9 | * @Date: 2018/8/250 19:27
10 | * @Description: xml aop测试
11 | */
12 | public class SpringAopAnnotationDemo {
13 |
14 | public static void main(String[] args) throws InterruptedException {
15 | ApplicationContext applicationContext
16 | = new ClassPathXmlApplicationContext("classpath:spring-chapter3-annotationaop.xml");
17 | Fruit apple = (Fruit) applicationContext.getBean("apple");
18 | Fruit banana = (Fruit) applicationContext.getBean("banana");
19 | apple.eat();
20 | System.out.println("-----休息一会儿-----");
21 | Thread.sleep(10000);
22 | banana.eat();
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/aop/jdk/annotation/readme.txt:
--------------------------------------------------------------------------------
1 | 注解方式实现Spring AOP
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/aop/jdk/xml/Apple.java:
--------------------------------------------------------------------------------
1 | package com.test.aop.jdk.xml;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2018/8/250 19:07
6 | * @Description: 苹果
7 | */
8 | public class Apple implements Fruit {
9 | /**
10 | * 吃水果
11 | */
12 | @Override
13 | public void eat() {
14 | try {
15 | //模拟吃苹果的过程
16 | Thread.sleep(10000);
17 | } catch (InterruptedException e) {
18 | e.printStackTrace();
19 | }
20 | System.out.println("吃苹果");
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/aop/jdk/xml/Banana.java:
--------------------------------------------------------------------------------
1 | package com.test.aop.jdk.xml;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2018/8/250 19:08
6 | * @Description: 香蕉
7 | */
8 | public class Banana implements Fruit {
9 | /**
10 | * 吃水果
11 | */
12 | @Override
13 | public void eat() {
14 | try {
15 | //模拟吃香蕉的过程
16 | Thread.sleep(10000);
17 | } catch (InterruptedException e) {
18 | e.printStackTrace();
19 | }
20 | System.out.println("吃香蕉");
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/aop/jdk/xml/Fruit.java:
--------------------------------------------------------------------------------
1 | package com.test.aop.jdk.xml;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2018/8/250 18:504
6 | * @Description: 水果接口
7 | */
8 | public interface Fruit {
9 | /**
10 | * 吃水果
11 | */
12 | void eat();
13 | }
14 |
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/aop/jdk/xml/FruitHandler.java:
--------------------------------------------------------------------------------
1 | package com.test.aop.jdk.xml;
2 |
3 | import java.text.SimpleDateFormat;
4 | import java.util.Date;
5 |
6 | /**
7 | * @Author: zhouguanya
8 | * @Date: 2018/8/250 19:100
9 | * @Description: 横切关注点,打印吃水果的时间
10 | */
11 | public class FruitHandler {
12 |
13 | /**
14 | * 打印开始吃水果的时间
15 | */
16 | public void startEatFruitDate() {
17 | SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
18 | String startEatDate = simpleDateFormat.format(new Date());
19 | System.out.println("开始吃水果的时间是:" + startEatDate);
20 | }
21 |
22 | /**
23 | * 打印吃完吃水果的时间
24 | */
25 | public void endEatFruitDate() {
26 | SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
27 | String endEatDate = simpleDateFormat.format(new Date());
28 | System.out.println("结束吃水果的时间是:" + endEatDate);
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/aop/jdk/xml/SpringAopXmlDemo.java:
--------------------------------------------------------------------------------
1 | package com.test.aop.jdk.xml;
2 |
3 | import org.springframework.context.ApplicationContext;
4 | import org.springframework.context.support.ClassPathXmlApplicationContext;
5 |
6 | /**
7 | * @Author: zhouguanya
8 | * @Date: 2018/8/250 19:27
9 | * @Description: xml aop测试
10 | */
11 | public class SpringAopXmlDemo {
12 |
13 | public static void main(String[] args) throws InterruptedException {
14 | ApplicationContext applicationContext
15 | = new ClassPathXmlApplicationContext("classpath:spring-chapter3-xmlaop.xml");
16 | Fruit apple = (Fruit) applicationContext.getBean("apple");
17 | Fruit banana = (Fruit) applicationContext.getBean("banana");
18 | apple.eat();
19 | System.out.println("-----休息一会儿-----");
20 | Thread.sleep(10000);
21 | banana.eat();
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/aop/jdk/xml/readme.txt:
--------------------------------------------------------------------------------
1 | xml方式实现Spring AOP
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/aspectj/advicetype/AllAspectDemo.java:
--------------------------------------------------------------------------------
1 | package com.test.aspectj.advicetype;
2 |
3 | import org.springframework.context.ApplicationContext;
4 | import org.springframework.context.support.ClassPathXmlApplicationContext;
5 |
6 | /**
7 | * @Author: zhouguanya
8 | * @Date: 2018/9/1
9 | * @Description: 测试各种类型的增强
10 | */
11 | public class AllAspectDemo {
12 |
13 | public static void main(String[] args) {
14 | ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-chapter3-aoptype.xml");
15 | Person person = (Person) context.getBean("person");
16 | person.say();
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/aspectj/advicetype/Person.java:
--------------------------------------------------------------------------------
1 | package com.test.aspectj.advicetype;
2 |
3 | import org.springframework.stereotype.Component;
4 |
5 | /**
6 | * @Author: zhouguanya
7 | * @Date: 2018/9/1
8 | * @Description: 一个Spring Bean
9 | */
10 | @Component
11 | public class Person {
12 |
13 | /**
14 | * 说话的方法
15 | */
16 | public void say() {
17 | System.out.println("Hello spring5");
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/aspectj/advicetype/readme.txt:
--------------------------------------------------------------------------------
1 | AspectJ不同增强类型demo
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/aspectj/expression/Factory.java:
--------------------------------------------------------------------------------
1 | package com.test.aspectj.expression;
2 |
3 | /**
4 | * @Author zhouguanya
5 | * @Date 2018/9/100
6 | * @Description 工厂接口
7 | */
8 | public interface Factory {
9 | /**
10 | * 制作产品
11 | */
12 | void make();
13 |
14 | /**
15 | * 运输
16 | */
17 | void delivery(String address);
18 | }
19 |
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/aspectj/expression/FoodFactory.java:
--------------------------------------------------------------------------------
1 | package com.test.aspectj.expression;
2 |
3 | import com.test.aspectj.expression.args.FreshFoodFactory;
4 | import org.springframework.stereotype.Component;
5 |
6 | /**
7 | * @Author zhouguanya
8 | * @Date 2018/9/100
9 | * @Description 食品工厂
10 | */
11 | @Component
12 | public class FoodFactory implements Factory {
13 | /**
14 | * 制作产品的方法
15 | */
16 | @Override
17 | public void make() {
18 | System.out.println("生产食品");
19 | }
20 |
21 | /**
22 | * 运输
23 | *
24 | * @param address
25 | */
26 | @Override
27 | public void delivery(String address) {
28 | System.out.println("销售食品至" + address);
29 | }
30 |
31 | /**
32 | * 测试@args注解
33 | */
34 | public void testArgsAnnotation(FreshFoodFactory freshFoodFactory) {
35 |
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/aspectj/expression/PhoneFactory.java:
--------------------------------------------------------------------------------
1 | package com.test.aspectj.expression;
2 |
3 | import com.test.aspectj.expression.annotation.Log;
4 | import org.springframework.stereotype.Component;
5 |
6 | /**
7 | * @Author zhouguanya
8 | * @Date 2018/9/100
9 | * @Description 手机工厂
10 | */
11 |
12 | @Component
13 | public class PhoneFactory implements Factory {
14 | /**
15 | * 制作产品的方法
16 | */
17 | @Override
18 | @Log
19 | public void make() {
20 | System.out.println("生产手机");
21 | }
22 |
23 | /**
24 | * 运输手机的方法
25 | */
26 | @Override
27 | public void delivery(String address) {
28 | System.out.println("运输手机至" + address);
29 | }
30 |
31 | /**
32 | * 测试@Within注解
33 | */
34 | public void testWithin() {
35 |
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/aspectj/expression/annotation/AnnotationAspect.java:
--------------------------------------------------------------------------------
1 | package com.test.aspectj.expression.annotation;
2 |
3 | import org.aspectj.lang.annotation.AfterReturning;
4 | import org.aspectj.lang.annotation.Aspect;
5 | import org.springframework.stereotype.Component;
6 |
7 | /**
8 | * @Author zhouguanya
9 | * @Date 2018/9/100
10 | * @Description 使用 @annotation() 来为所有加了 @Log 注解的方法织入增强
11 | */
12 | @Aspect
13 | public class AnnotationAspect {
14 | @AfterReturning("@annotation(com.test.aspectj.expression.annotation.Log)")
15 | public void log() {
16 | System.out.println("打印日志");
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/aspectj/expression/annotation/AspectJExpressionDemo.java:
--------------------------------------------------------------------------------
1 | package com.test.aspectj.expression.annotation;
2 |
3 | import com.test.aspectj.expression.Factory;
4 | import org.springframework.context.ApplicationContext;
5 | import org.springframework.context.support.ClassPathXmlApplicationContext;
6 |
7 | /**
8 | * @Author zhouguanya
9 | * @Date 2018/9/100
10 | * @Description 测试ExecutionAspect切面
11 | */
12 | public class AspectJExpressionDemo {
13 | public static void main(String[] args) {
14 | ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-chapter3-aspectjannotationexpression.xml");
15 | Factory foodFactory = (Factory) context.getBean("foodFactory");
16 | foodFactory.make();
17 | System.out.println("-----分割线-----");
18 | Factory phoneFactory = (Factory) context.getBean("phoneFactory");
19 | phoneFactory.make();
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/aspectj/expression/annotation/Log.java:
--------------------------------------------------------------------------------
1 | package com.test.aspectj.expression.annotation;
2 |
3 | import java.lang.annotation.ElementType;
4 | import java.lang.annotation.Retention;
5 | import java.lang.annotation.RetentionPolicy;
6 | import java.lang.annotation.Target;
7 |
8 | /**
9 | * @Author zhouguanya
10 | * @Date 2018/9/100
11 | * @Description 自定义日志注解
12 | */
13 | @Retention(RetentionPolicy.RUNTIME)
14 | @Target(ElementType.METHOD)
15 | public @interface Log {
16 | boolean value() default true;
17 | }
18 |
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/aspectj/expression/annotation/readme.txt:
--------------------------------------------------------------------------------
1 | @annotation() 测试demo
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/aspectj/expression/args/ArgsAspect.java:
--------------------------------------------------------------------------------
1 | package com.test.aspectj.expression.args;
2 |
3 | import org.aspectj.lang.annotation.After;
4 | import org.aspectj.lang.annotation.Aspect;
5 | import org.aspectj.lang.annotation.Before;
6 |
7 | /**
8 | * @Author zhouguanya
9 | * @Date 2018/9/100
10 | * @Description args和@args切面逻辑
11 | */
12 | @Aspect
13 | public class ArgsAspect {
14 |
15 | @Before("args(java.lang.String)")
16 | public void before() {
17 | System.out.println("args匹配方法入参是String的方法");
18 | }
19 |
20 | @After("@args(com.test.aspectj.expression.args.Listen)")
21 | public void after() {
22 | System.out.println("@args匹配到方法执行了");
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/aspectj/expression/args/FreshFoodFactory.java:
--------------------------------------------------------------------------------
1 | package com.test.aspectj.expression.args;
2 |
3 | import com.test.aspectj.expression.FoodFactory;
4 | import org.springframework.stereotype.Component;
5 |
6 | /**
7 | * @Author zhouguanya
8 | * @Date 2018/9/100
9 | * @Description 新鲜食品工厂
10 | */
11 | @Listen
12 | @Component
13 | public class FreshFoodFactory extends FoodFactory {
14 |
15 | }
16 |
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/aspectj/expression/args/FrozenFoodFactory.java:
--------------------------------------------------------------------------------
1 | package com.test.aspectj.expression.args;
2 |
3 | import org.springframework.stereotype.Component;
4 |
5 | /**
6 | * @Author zhouguanya
7 | * @Date 2018/9/100
8 | * @Description 冷冻食品工厂
9 | */
10 | @Component
11 | public class FrozenFoodFactory extends FreshFoodFactory {
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/aspectj/expression/args/Listen.java:
--------------------------------------------------------------------------------
1 | package com.test.aspectj.expression.args;
2 |
3 | import java.lang.annotation.*;
4 |
5 | /**
6 | * @Author zhouguanya
7 | * @Date 2018/9/100
8 | * @Description 监听注解
9 | */
10 | @Retention(RetentionPolicy.RUNTIME)
11 | @Target({ ElementType.TYPE, ElementType.METHOD })
12 | @Documented
13 | public @interface Listen {
14 | String value() default "";
15 | }
16 |
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/aspectj/expression/args/readme.txt:
--------------------------------------------------------------------------------
1 | args 和 @args 测试demo
2 |
3 | args()
4 | 该函数接收一个类名,表示目标类方法入参对象是指定类(包含子类)时,切点匹配。
5 | 比如args(com.test.Waiter) 表示运行时入参是Waiter类型的方法,它和execution(* *(com.test.Waiter))的区别:
6 | 在于后者是这对类方法的签名而言的,而前者是针对运行时的入参类型而言。
7 |
8 | @args()
9 | 该函数接收一个注解类的类名,当方法的运行时入参对象标注了指定的注解时,匹配切点。
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/aspectj/expression/execution/AspectJExpressionDemo.java:
--------------------------------------------------------------------------------
1 | package com.test.aspectj.expression.execution;
2 |
3 | import com.test.aspectj.expression.Factory;
4 | import org.springframework.context.ApplicationContext;
5 | import org.springframework.context.support.ClassPathXmlApplicationContext;
6 |
7 | /**
8 | * @Author zhouguanya
9 | * @Date 2018/9/100
10 | * @Description 测试execution增强
11 | */
12 | public class AspectJExpressionDemo {
13 | public static void main(String[] args) {
14 | ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-chapter3-aspectjexecutionexpression.xml");
15 | Factory foodFactory = (Factory) context.getBean("foodFactory");
16 | foodFactory.make();
17 | System.out.println("-----分割线-----");
18 | Factory phoneFactory = (Factory) context.getBean("phoneFactory");
19 | phoneFactory.make();
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/aspectj/expression/execution/ExecutionAspect.java:
--------------------------------------------------------------------------------
1 | package com.test.aspectj.expression.execution;
2 |
3 | import org.aspectj.lang.annotation.AfterReturning;
4 | import org.aspectj.lang.annotation.Aspect;
5 | import org.springframework.stereotype.Component;
6 |
7 | /**
8 | * @Author zhouguanya
9 | * @Date 2018/9/100
10 | * @Description 使用execution 来为所有Factory接口的实现类织入增强
11 | */
12 | @Aspect
13 | public class ExecutionAspect {
14 | @AfterReturning("execution(* com.test.aspectj.expression.Factory.*(..))")
15 | public void make() {
16 | System.out.println("make方法执行了");
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/aspectj/expression/execution/readme.txt:
--------------------------------------------------------------------------------
1 | execution() 测试demo
2 |
3 |
4 | execution(public * *(..)) 表示匹配所有目标类的 public 方法 。 第一个 * 代表任意返回类型,第二个 * 代表方法名,而 .. 代表任意入参的方法。
5 | execution(* m*(..)) 表示匹配目标类所有以 m 为前缀的方法。 第一个*代表任意返回类型,而m*代表任意以 m 为前缀的方法。这里匹配 make()方法。
6 |
7 |
8 | execution(* com.test.aspectj.expression.Factory.*(..)) 匹配 接口的所有方法,即这里的 make() 。第一个*代表任意返回类型, com.test.aspectj.expression.Factory.* 表示 Factory 接口的所有方法。
9 |
10 |
11 | .* 包中的所有类,不包括子孙包中的类。
12 | ..* 包中以及子孙包的所有类。
13 |
14 |
15 | * 表示任意类型参数。
16 | .. 表示任意类型参数且参数不限个数。
17 |
18 |
19 | execution(* make(int,String)) 这会匹配 make(int,String) 方法。如果方法中的入参类型是 java.lang 包下的类,那么可以直接使用类名;如果是其他类型,那么必须使用全限定名。
20 | execution(* make(int,*)) 匹配 make() 方法,该方法的第一个入参是 int,第二个参数可以是任意类型。
21 | execution(* make(int,..)) 匹配 make() 方法,该方法的第一个入参是 int,后面可以有任意类型的入参且参数个数不限。
22 | execution(* make(Object+)) 匹配 make() 方法,方法只有一个入参,它可以是 Object 类型或该类型的子类。
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/aspectj/expression/readme.txt:
--------------------------------------------------------------------------------
1 | 本节讲述切入点表达式
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/aspectj/expression/thisexpression/DefaultListener.java:
--------------------------------------------------------------------------------
1 | package com.test.aspectj.expression.thisexpression;
2 |
3 |
4 | /**
5 | * @Author zhouguanya
6 | * @Date 2018/9/13
7 | * @Description Listener接口实现类
8 | */
9 | public class DefaultListener implements Listener {
10 | /**
11 | * 监听
12 | */
13 | @Override
14 | public void listen() {
15 | System.out.println("开始监听");
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/aspectj/expression/thisexpression/Listener.java:
--------------------------------------------------------------------------------
1 | package com.test.aspectj.expression.thisexpression;
2 |
3 | /**
4 | * @Author zhouguanya
5 | * @Date 2018/9/13
6 | * @Description 引介切面要实现的接口
7 | */
8 | public interface Listener {
9 | /**
10 | * 监听
11 | */
12 | void listen();
13 | }
14 |
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/aspectj/expression/thisexpression/ListenerAspect.java:
--------------------------------------------------------------------------------
1 | package com.test.aspectj.expression.thisexpression;
2 |
3 | import org.aspectj.lang.annotation.Aspect;
4 | import org.aspectj.lang.annotation.DeclareParents;
5 | import org.springframework.core.Ordered;
6 |
7 | /**
8 | * @Author zhouguanya
9 | * @Date 2018/9/13
10 | * @Description 为FoodFactory添加Listener接口的切面
11 | */
12 | @Aspect
13 | public class ListenerAspect implements Ordered {
14 |
15 | /**
16 | * 为FoodFactory添加接口实现,要实现的接口是Listener,接口的默认实现是DefaultListener
17 | */
18 | @DeclareParents(value = "com.test.aspectj.expression.FoodFactory", defaultImpl = DefaultListener.class)
19 | public static Listener listener;
20 |
21 | /**
22 | * 如果有多个切面,注意多切面织入的顺序
23 | */
24 | @Override
25 | public int getOrder() {
26 | return 2;
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/aspectj/expression/thisexpression/ThisExpressionDemo.java:
--------------------------------------------------------------------------------
1 | package com.test.aspectj.expression.thisexpression;
2 |
3 | import com.test.aspectj.expression.FoodFactory;
4 | import org.springframework.context.ApplicationContext;
5 | import org.springframework.context.support.ClassPathXmlApplicationContext;
6 |
7 | /**
8 | * @Author zhouguanya
9 | * @Date 2018/9/100
10 | * @Description 测试
11 | */
12 | public class ThisExpressionDemo {
13 | public static void main(String[] args) {
14 | ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-chapter3-aspectjthisexpression.xml");
15 | FoodFactory foodFactory = (FoodFactory) context.getBean("foodFactory");
16 | foodFactory.make();
17 | System.out.println("-----分割线-----");
18 | Listener listener = (Listener) foodFactory;
19 | listener.listen();
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/aspectj/expression/thisexpression/readme.txt:
--------------------------------------------------------------------------------
1 | 一般情况下,使用this()和target()来匹配定义切点,二者是等效的
2 |
3 | 二者的区别体现在通过引介切面产生代理对象时的具体表现
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/aspectj/expression/within/IPhoneFactory.java:
--------------------------------------------------------------------------------
1 | package com.test.aspectj.expression.within;
2 |
3 | import org.springframework.stereotype.Component;
4 |
5 | /**
6 | * @Author zhouguanya
7 | * @Date 2018/9/13
8 | * @Description iPhone手机工厂
9 | */
10 | @Component(value = "iPhoneFactory")
11 | public class IPhoneFactory extends MobilePhoneFactory {
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/aspectj/expression/within/MobilePhoneFactory.java:
--------------------------------------------------------------------------------
1 | package com.test.aspectj.expression.within;
2 |
3 | import com.test.aspectj.expression.PhoneFactory;
4 | import org.springframework.stereotype.Component;
5 |
6 | /**
7 | * @Author zhouguanya
8 | * @Date 2018/9/13
9 | * @Description 手机工厂
10 | */
11 | @Monitor
12 | @Component
13 | public class MobilePhoneFactory extends PhoneFactory {
14 | @Override
15 | public void testWithin() {
16 |
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/aspectj/expression/within/Monitor.java:
--------------------------------------------------------------------------------
1 | package com.test.aspectj.expression.within;
2 |
3 | import java.lang.annotation.*;
4 |
5 | /**
6 | * @Author zhouguanya
7 | * @Date 2018/9/12
8 | * @Description 监控
9 | */
10 | @Retention(RetentionPolicy.RUNTIME)
11 | @Target({ ElementType.TYPE, ElementType.METHOD })
12 | @Documented
13 | public @interface Monitor {
14 | String value() default "";
15 | }
16 |
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/aspectj/expression/within/WithinAspect.java:
--------------------------------------------------------------------------------
1 | package com.test.aspectj.expression.within;
2 |
3 | import org.aspectj.lang.annotation.After;
4 | import org.aspectj.lang.annotation.Aspect;
5 | import org.aspectj.lang.annotation.Before;
6 |
7 | /**
8 | * @Author zhouguanya
9 | * @Date 2018/9/100
10 | * @Description 测试within()和@ within()的切面
11 | */
12 | @Aspect
13 | public class WithinAspect {
14 | @Before("within(com.test.aspectj.expression.FoodFactory)")
15 | public void before() {
16 | System.out.println("方法执行前增强");
17 | }
18 |
19 | @After("@within(com.test.aspectj.expression.within.Monitor)")
20 | public void after() {
21 | System.out.println("@within匹配到,执行增强");
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/aspectj/expression/within/readme.txt:
--------------------------------------------------------------------------------
1 | within() 定义的连接点最小范围是类(不是接口)
2 |
3 | execution() 定义的连接点最小范围可以到方法入参
4 |
5 | 所以 execution() 涵盖了 within() 功能。
6 |
7 |
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/aspectj/readme.txt:
--------------------------------------------------------------------------------
1 | AspectJ实现AOP demo
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/cglibdynamicproxy/Cat.java:
--------------------------------------------------------------------------------
1 | package com.test.cglibdynamicproxy;
2 |
3 | /**
4 | * @Author zhouguanya
5 | * @Date 2018/8/21
6 | * @Description 被代理类
7 | */
8 | public class Cat {
9 | /**
10 | * 方法
11 | */
12 | public void cry() {
13 | System.out.println("喵喵喵");
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/cglibdynamicproxy/CglibDynamicProxyDemo.java:
--------------------------------------------------------------------------------
1 | package com.test.cglibdynamicproxy;
2 |
3 | import org.springframework.cglib.proxy.Enhancer;
4 |
5 | /**
6 | * @Author zhouguanya
7 | * @Date 2018/8/21
8 | * @Description 测试Cglib
9 | */
10 | public class CglibDynamicProxyDemo {
11 |
12 | public static void main(String[] args) {
13 | Enhancer enhancer = new Enhancer();
14 | //被代理类:Cat
15 | enhancer.setSuperclass(Cat.class);
16 | //设置回调
17 | enhancer.setCallback(new CatMethodInterceptor());
18 | //生成代理对象
19 | Cat cat = (Cat) enhancer.create();
20 | //调用代理类的方法
21 | cat.cry();
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/cglibdynamicproxy/readme.txt:
--------------------------------------------------------------------------------
1 | cglig 动态代理
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/jdkdynamicproxy/Animal.java:
--------------------------------------------------------------------------------
1 | package com.test.jdkdynamicproxy;
2 |
3 | /**
4 | * @Author zhouguanya
5 | * @Date 2018/8/20
6 | * @Description 接口
7 | */
8 | public interface Animal {
9 | /**
10 | * 接口方法
11 | */
12 | void eat();
13 | }
14 |
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/jdkdynamicproxy/Dog.java:
--------------------------------------------------------------------------------
1 | package com.test.jdkdynamicproxy;
2 |
3 | /**
4 | * @Author zhouguanya
5 | * @Date 2018/8/20
6 | * @Description 接口实现类
7 | */
8 | public class Dog implements Animal {
9 | /**
10 | * 接口方法
11 | */
12 | @Override
13 | public void eat() {
14 | System.out.println("Dog需要吃骨头");
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/jdkdynamicproxy/JDKDynamicProxyDemo.java:
--------------------------------------------------------------------------------
1 | package com.test.jdkdynamicproxy;
2 |
3 | /**
4 | * @Author zhouguanya
5 | * @Date 2018/8/20
6 | * @Description 测试
7 | */
8 | public class JDKDynamicProxyDemo {
9 | public static void main(String[] args) {
10 | //被代理对象
11 | Dog dog = new Dog();
12 | //动态代理类对象
13 | AnimalInvocationHandler animalInvocationHandler = new AnimalInvocationHandler();
14 | //代理对象
15 | Animal proxy = (Animal) animalInvocationHandler.bind(dog);
16 | proxy.eat();
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/jdkdynamicproxy/readme.txt:
--------------------------------------------------------------------------------
1 | JDK 动态代理
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/sourcecodelearning/AopSourceCodeLearningDemo.java:
--------------------------------------------------------------------------------
1 | package com.test.sourcecodelearning;
2 |
3 | import org.springframework.context.ApplicationContext;
4 | import org.springframework.context.support.ClassPathXmlApplicationContext;
5 |
6 | /**
7 | * @Author zhouguanya
8 | * @Date 2018/100/3
9 | * @Description 测试
10 | */
11 | public class AopSourceCodeLearningDemo {
12 | public static void main(String[] args) {
13 | //加载配置文件
14 | ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-chapter3-sourcecodelearning.xml");
15 | //获取目标对象
16 | Log target = (Log) context.getBean("proxyFactoryBean");
17 | //执行目标对象的方法
18 | target.printLog();
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/sourcecodelearning/Log.java:
--------------------------------------------------------------------------------
1 | package com.test.sourcecodelearning;
2 |
3 | /**
4 | * @Author zhouguanya
5 | * @Date 2018/100/3
6 | * @Description 定义接口
7 | */
8 | public interface Log {
9 |
10 | /**
11 | * 打印日志
12 | */
13 | void printLog();
14 | }
15 |
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/sourcecodelearning/LogAroundAdvice.java:
--------------------------------------------------------------------------------
1 | package com.test.sourcecodelearning;
2 |
3 | import org.aopalliance.intercept.MethodInterceptor;
4 | import org.aopalliance.intercept.MethodInvocation;
5 |
6 | import java.text.SimpleDateFormat;
7 | import java.util.Date;
8 |
9 | /**
10 | * @Author zhouguanya
11 | * @Date 2018/100/3
12 | * @Description 通知
13 | */
14 | public class LogAroundAdvice implements MethodInterceptor {
15 |
16 | @Override
17 | public Object invoke(MethodInvocation invocation) throws Throwable {
18 | SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss SSS");
19 | System.out.println("方法执行开始时间:" + dateFormat.format(new Date()));
20 | invocation.proceed();
21 | System.out.println("方法执行结束时间:" + dateFormat.format(new Date()));
22 | return null;
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/sourcecodelearning/Target.java:
--------------------------------------------------------------------------------
1 | package com.test.sourcecodelearning;
2 |
3 | /**
4 | * @Author zhouguanya
5 | * @Date 2018/100/3
6 | * @Description 目标对象
7 | */
8 | public class Target implements Log {
9 |
10 | /**
11 | * 操作方法
12 | */
13 | @Override
14 | public void printLog() {
15 | try {
16 | //模拟一个耗时1秒的操作
17 | Thread.sleep(10000);
18 | } catch (InterruptedException e) {
19 | e.printStackTrace();
20 | }
21 | System.out.println("执行一些操作");
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/springadvicetype/Waiter.java:
--------------------------------------------------------------------------------
1 | package com.test.springadvicetype;
2 |
3 | import org.springframework.stereotype.Component;
4 |
5 | import java.text.SimpleDateFormat;
6 | import java.util.Date;
7 |
8 | /**
9 | * @Author zhouguanya
10 | * @Date 2018/9/2
11 | * @Description 服务员类
12 | */
13 | @Component
14 | public class Waiter {
15 | /**
16 | * 服务
17 | * @param name
18 | */
19 | public String serve(String name) {
20 | System.out.println(name + ",您好,很高兴为您服务。");
21 | SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
22 | return name + ",您好,现在是北京时间" + format.format(new Date());
23 | }
24 |
25 | /**
26 | * 开车
27 | * @param name
28 | */
29 | public void driving(String name) {
30 | throw new RuntimeException(name + ",您好,禁止酒后驾车!");
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/springadvicetype/afterretuningadvice/SpringAfterReturningAdvice.java:
--------------------------------------------------------------------------------
1 | package com.test.springadvicetype.afterretuningadvice;
2 |
3 | import org.springframework.aop.AfterReturningAdvice;
4 | import org.springframework.stereotype.Component;
5 |
6 | import java.lang.reflect.Method;
7 |
8 | /**
9 | * @Author zhouguanya
10 | * @Date 2018/9/2
11 | * @Description 后置增强
12 | */
13 | @Component
14 | public class SpringAfterReturningAdvice implements AfterReturningAdvice {
15 | @Override
16 | public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
17 | String methodName = method.getName();
18 | System.out.printf("AfterReturningAdvice增强的方法返回值是:%s%n", returnValue);
19 | System.out.printf("AfterReturningAdvice增强的方法是:%s%n", methodName);
20 | System.out.printf("AfterReturningAdvice增强的方法的参数是:%s%n", args[0]);
21 | System.out.printf("AfterReturningAdvice增强的对象是:%s%n", target);
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/springadvicetype/afterretuningadvice/readme.txt:
--------------------------------------------------------------------------------
1 | 后置增强测试
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/springadvicetype/beforeadvice/SpringBeforeAdvice.java:
--------------------------------------------------------------------------------
1 | package com.test.springadvicetype.beforeadvice;
2 |
3 | import org.springframework.aop.MethodBeforeAdvice;
4 | import org.springframework.stereotype.Component;
5 |
6 | import java.lang.reflect.Method;
7 |
8 | /**
9 | * @Author zhouguanya
10 | * @Date 2018/9/2
11 | * @Description 前置增强实现类
12 | */
13 | @Component
14 | public class SpringBeforeAdvice implements MethodBeforeAdvice {
15 | @Override
16 | public void before(Method method, Object[] args, Object target) throws Throwable {
17 | String methodName = method.getName();
18 | System.out.printf("MethodBeforeAdvice增强的方法是%s%n", methodName);
19 | System.out.printf("MethodBeforeAdvice增强的方法的参数是%s%n", args[0]);
20 | System.out.printf("MethodBeforeAdvice增强的对象是%s%n", target);
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/springadvicetype/beforeadvice/readme.txt:
--------------------------------------------------------------------------------
1 | 前置增强测试
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/springadvicetype/introductioninterceptor/Management.java:
--------------------------------------------------------------------------------
1 | package com.test.springadvicetype.introductioninterceptor;
2 |
3 | /**
4 | * @Author zhouguanya
5 | * @Date 2018/9/2
6 | * @Description
7 | */
8 | public interface Management {
9 | /**
10 | * 管理
11 | * @param name
12 | */
13 | void manage(String name);
14 | }
15 |
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/springadvicetype/introductioninterceptor/Manager.java:
--------------------------------------------------------------------------------
1 | package com.test.springadvicetype.introductioninterceptor;
2 |
3 | import org.springframework.aop.support.DelegatingIntroductionInterceptor;
4 |
5 | /**
6 | * @Author zhouguanya
7 | * @Date 2018/9/2
8 | * @Description 经理类
9 | */
10 | public class Manager extends DelegatingIntroductionInterceptor implements Management {
11 |
12 | @Override
13 | public void manage(String name) {
14 | System.out.println(name + ",您好,我是经理,负责管理服务员");
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/springadvicetype/introductioninterceptor/SpringIntroductionInterceptorDemo.java:
--------------------------------------------------------------------------------
1 | package com.test.springadvicetype.introductioninterceptor;
2 |
3 | import com.test.springadvicetype.Waiter;
4 | import org.springframework.context.ApplicationContext;
5 | import org.springframework.context.support.ClassPathXmlApplicationContext;
6 |
7 | /**
8 | * @Author zhouguanya
9 | * @Date 2018/9/2
10 | * @Description Spring引介增强测试
11 | */
12 | public class SpringIntroductionInterceptorDemo {
13 |
14 | public static void main(String[] args) {
15 | ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-chapter3-springintroductioninterceptor.xml");
16 | Waiter waiterProxy = (Waiter) context.getBean("waiterProxy");
17 | waiterProxy.serve("Michael");
18 | Management manager = (Management)waiterProxy;
19 | manager.manage("Michael");
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/springadvicetype/introductioninterceptor/readme.txt:
--------------------------------------------------------------------------------
1 | 引介增强测试
2 | 引介增强的配置需要制定引介增强所实现的接口,其次需要将proxyTargetClass设置为true。
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/springadvicetype/methodinterceptor/SpringMethodInterceptor.java:
--------------------------------------------------------------------------------
1 | package com.test.springadvicetype.methodinterceptor;
2 |
3 | import org.aopalliance.intercept.MethodInterceptor;
4 | import org.aopalliance.intercept.MethodInvocation;
5 | import org.springframework.stereotype.Service;
6 |
7 | /**
8 | * @Author zhouguanya
9 | * @Date 2018/9/2
10 | * @Description 环绕增强
11 | */
12 | @Service
13 | public class SpringMethodInterceptor implements MethodInterceptor {
14 | @Override
15 | public Object invoke(MethodInvocation invocation) throws Throwable {
16 | // 前置增强
17 | System.out.println("前置增强执行了");
18 | // 通过反射机制调用目标方法
19 | Object obj = invocation.proceed();
20 | // 后置增强
21 | System.out.println("后置增强执行了");
22 | return obj;
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/springadvicetype/methodinterceptor/readme.txt:
--------------------------------------------------------------------------------
1 | 环绕增强测试
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/springadvicetype/throwsadvice/SpringThrowsAdvice.java:
--------------------------------------------------------------------------------
1 | package com.test.springadvicetype.throwsadvice;
2 |
3 | import org.springframework.aop.ThrowsAdvice;
4 | import org.springframework.stereotype.Component;
5 |
6 | /**
7 | * @Author zhouguanya
8 | * @Date 2018/9/2
9 | * @Description 异常抛出增强
10 | */
11 | @Component
12 | public class SpringThrowsAdvice implements ThrowsAdvice {
13 |
14 | public void afterThrowing(Exception e) throws Throwable{
15 | System.out.printf("异常抛出增强执行:%s%n", e);
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/chapter3/src/main/java/com/test/springadvicetype/throwsadvice/readme.txt:
--------------------------------------------------------------------------------
1 | 异常抛出增强测试
--------------------------------------------------------------------------------
/chapter3/src/main/resources/spring-chapter3-annotationcglibaop.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/chapter3/src/main/resources/spring-chapter3-aoptype.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/chapter3/src/main/resources/spring-chapter3-aspectjannotationexpression.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/chapter3/src/main/resources/spring-chapter3-aspectjargsexpression.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/chapter3/src/main/resources/spring-chapter3-aspectjexecutionexpression.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/chapter3/src/main/resources/spring-chapter3-aspectjtargetexpression.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/chapter3/src/main/resources/spring-chapter3-aspectjthisexpression.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/chapter3/src/main/resources/spring-chapter3-aspectjwithinexpression.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/chapter3/src/main/resources/spring-chapter3-springaoptype.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/chapter5/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | project.demo
7 | com.test.spring5
8 | 1.0-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | chapter5
13 |
14 |
15 |
--------------------------------------------------------------------------------
/chapter5/src/main/java/com/test/jdk8/defaultmethod/Bus.java:
--------------------------------------------------------------------------------
1 | package com.test.jdk8.defaultmethod;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2018/12/23
6 | * @Description:
7 | */
8 | public class Bus implements Vehicle, Whistle {
9 | /**
10 | * 同时实现Vehicle,Whistle接口,需要重写默认方法
11 | */
12 | public void print() {
13 | System.out.println("我是一辆巴士");
14 | }
15 |
16 | /**
17 | * 实现抽象方法
18 | */
19 | @Override
20 | public void drive() {
21 | System.out.println("开一辆巴士");
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/chapter5/src/main/java/com/test/jdk8/defaultmethod/Car.java:
--------------------------------------------------------------------------------
1 | package com.test.jdk8.defaultmethod;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2018/12/23
6 | * @Description: Vehicle接口实现类Car
7 | */
8 | public class Car implements Vehicle {
9 | /**
10 | * 抽象方法
11 | */
12 | @Override
13 | public void drive() {
14 | System.out.println("开一辆轿车");
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/chapter5/src/main/java/com/test/jdk8/defaultmethod/DefaultMethodDemo.java:
--------------------------------------------------------------------------------
1 | package com.test.jdk8.defaultmethod;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2018/12/23
6 | * @Description:
7 | */
8 | public class DefaultMethodDemo {
9 | public static void main(String[] args) {
10 | Vehicle car = new Car();
11 | // 调用Vehicle接口默认方法
12 | car.print();
13 | // 调用Car中重写的drive方法
14 | car.drive();
15 | Bus bus = new Bus();
16 | bus.print();
17 | bus.drive();
18 | Whistle.horn();
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/chapter5/src/main/java/com/test/jdk8/defaultmethod/Vehicle.java:
--------------------------------------------------------------------------------
1 | package com.test.jdk8.defaultmethod;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2018/12/23
6 | * @Description:
7 | */
8 | public interface Vehicle {
9 | /**
10 | * 默认方法
11 | */
12 | default void print() {
13 | System.out.println("我是一辆车");
14 | }
15 |
16 | /**
17 | * 抽象方法
18 | */
19 | void drive();
20 | }
21 |
--------------------------------------------------------------------------------
/chapter5/src/main/java/com/test/jdk8/defaultmethod/Whistle.java:
--------------------------------------------------------------------------------
1 | package com.test.jdk8.defaultmethod;
2 |
3 | /**
4 | * @Author: zhouguanya
5 | * @Date: 2018/12/23
6 | * @Description:
7 | */
8 | public interface Whistle {
9 | /**
10 | * 默认方法
11 | */
12 | default void print() {
13 | System.out.println("我要鸣笛");
14 | }
15 |
16 | /**
17 | * 静态方法
18 | */
19 | static void horn(){
20 | System.out.println("按喇叭~");
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/chapter5/src/main/java/com/test/jdk8/lambda/Java8LambdaInThreadDemo.java:
--------------------------------------------------------------------------------
1 | package com.test.jdk8.lambda;
2 |
3 | /**
4 | * @Author zhouguanya
5 | * @Date 2018/10/12
6 | * @Description 测试Lambda表达式配合线程使用
7 | */
8 | public class Java8LambdaInThreadDemo {
9 |
10 | public static void main(String[] args) throws Exception {
11 | // 不使用Lambda表达式,使用匿名类
12 | // 或者定义一个类实现Runnable接口
13 | new Thread(new Runnable() {
14 | @Override
15 | public void run () {
16 | System.out.println("线程1");
17 | }
18 | }).start();
19 |
20 | // 使用lambda表达式
21 | new Thread(() ->System.out.println("线程2")).start();
22 |
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/chapter5/src/main/java/com/test/jdk8/lambda/Java8LambdaScopeDemo.java:
--------------------------------------------------------------------------------
1 | package com.test.jdk8.lambda;
2 |
3 | /**
4 | * @Author zhouguanya
5 | * @Date 2018/10/11
6 | * @Description Lambda作用域测试
7 | */
8 | public class Java8LambdaScopeDemo {
9 |
10 | public static void main(String[] args) {
11 | final String salutation = "Hello ";
12 | String myName = "I am Lambda ~";
13 | String today = "2018/10/11";
14 | SayHello greetingService = message -> {
15 | System.out.println(salutation + message + myName);
16 | //此处修改today将会出现编译错误
17 | //today = "2018/10/12";
18 | //此处定义局部变量myName将会出现编译错误
19 | //String myName = "Java";
20 | };
21 | greetingService.say("World !");
22 | }
23 |
24 | /**
25 | * 打招呼接口
26 | */
27 | interface SayHello {
28 | /**
29 | * 打招呼方法
30 | * @param message
31 | */
32 | void say(String message);
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/chapter5/src/main/java/com/test/jdk8/lambda/Person.java:
--------------------------------------------------------------------------------
1 | package com.test.jdk8.lambda;
2 |
3 | /**
4 | * @Author zhouguanya
5 | * @Date 2018/10/12
6 | * @Description
7 | */
8 | public class Person {
9 | /**
10 | * 姓名
11 | */
12 | public String name;
13 | /**
14 | * 年龄
15 | */
16 | public int age;
17 |
18 | /**
19 | * 构造器
20 | */
21 | public Person(String name, int age) {
22 | this.name = name;
23 | this.age = age;
24 | }
25 |
26 | @Override
27 | public String toString() {
28 | return this.name + ":" + this.age;
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/chapter6-webflux/.gitignore:
--------------------------------------------------------------------------------
1 | /target/
2 | !.mvn/wrapper/maven-wrapper.jar
3 |
4 | ### STS ###
5 | .apt_generated
6 | .classpath
7 | .factorypath
8 | .project
9 | .settings
10 | .springBeans
11 | .sts4-cache
12 |
13 | ### IntelliJ IDEA ###
14 | .idea
15 | *.iws
16 | *.iml
17 | *.ipr
18 |
19 | ### NetBeans ###
20 | /nbproject/private/
21 | /build/
22 | /nbbuild/
23 | /dist/
24 | /nbdist/
25 | /.nb-gradle/
26 | *.iml
--------------------------------------------------------------------------------
/chapter6-webflux/.mvn/wrapper/maven-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/online-demo/spring5projectdemo/4574663109e7e0d732733b6f694f211ba5ed7ba3/chapter6-webflux/.mvn/wrapper/maven-wrapper.jar
--------------------------------------------------------------------------------
/chapter6-webflux/.mvn/wrapper/maven-wrapper.properties:
--------------------------------------------------------------------------------
1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.5.4/apache-maven-3.5.4-bin.zip
2 |
--------------------------------------------------------------------------------
/chapter6-webflux/src/main/java/com/test/chapter6webflux/Chapter6WebfluxApplication.java:
--------------------------------------------------------------------------------
1 | package com.test.chapter6webflux;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | @SpringBootApplication
7 | public class Chapter6WebfluxApplication {
8 |
9 | public static void main(String[] args) {
10 | SpringApplication.run(Chapter6WebfluxApplication.class, args);
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/chapter6-webflux/src/main/java/com/test/chapter6webflux/model/User.java:
--------------------------------------------------------------------------------
1 | package com.test.chapter6webflux.model;
2 |
3 | /**
4 | * @Author zhouguanya
5 | * @Date 2018/11/07
6 | * @Description user实体
7 | */
8 | public class User {
9 | /** id */
10 | private Long id;
11 | /** 姓名 */
12 | private String name;
13 |
14 | /** 构造器 */
15 | public User(Long uid, String name) {
16 | this.id = uid;
17 | this.name = name;
18 | }
19 |
20 | public Long getId() {
21 | return id;
22 | }
23 |
24 | public void setId(Long id) {
25 | this.id = id;
26 | }
27 |
28 | public String getName() {
29 | return name;
30 | }
31 |
32 | public void setName(String name) {
33 | this.name = name;
34 | }
35 |
36 | }
37 |
--------------------------------------------------------------------------------
/chapter6-webflux/src/main/java/com/test/chapter6webflux/service/UserService.java:
--------------------------------------------------------------------------------
1 | package com.test.chapter6webflux.service;
2 |
3 | import com.test.chapter6webflux.model.User;
4 | import reactor.core.publisher.Flux;
5 | import reactor.core.publisher.Mono;
6 |
7 | /**
8 | * @Author zhouguanya
9 | * @Date 2018/11/07
10 | * @Description user接口
11 | */
12 | public interface UserService {
13 | /**
14 | * 查询所有用户
15 | */
16 | Flux queryAllUserList();
17 | /**
18 | * 根据id查询用户
19 | */
20 | Mono queryUserById(Long id);
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/chapter6-webflux/src/main/resources/application.properties:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/online-demo/spring5projectdemo/4574663109e7e0d732733b6f694f211ba5ed7ba3/chapter6-webflux/src/main/resources/application.properties
--------------------------------------------------------------------------------
/chapter6-webflux/src/test/java/com/test/chapter6webflux/Chapter6WebfluxApplicationTests.java:
--------------------------------------------------------------------------------
1 | package com.test.chapter6webflux;
2 |
3 | import org.junit.Test;
4 | import org.junit.runner.RunWith;
5 | import org.springframework.boot.test.context.SpringBootTest;
6 | import org.springframework.test.context.junit4.SpringRunner;
7 |
8 | @RunWith(SpringRunner.class)
9 | @SpringBootTest
10 | public class Chapter6WebfluxApplicationTests {
11 |
12 | @Test
13 | public void contextLoads() {
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/chapter6/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | project.demo
7 | com.test.spring5
8 | 1.0-SNAPSHOT
9 |
10 | 4.0.0
11 | chapter6
12 | ${spring5.chapter6.version}
13 | jar
14 |
--------------------------------------------------------------------------------
/chapter6/src/main/java/com/test/reactor/FilterOperationDemo.java:
--------------------------------------------------------------------------------
1 | package com.test.reactor;
2 |
3 | import reactor.core.publisher.Flux;
4 | import reactor.test.StepVerifier;
5 |
6 | /**
7 | * @Author zhouguanya
8 | * @Date 2018/10/31
9 | * @Description filter操作符测试
10 | */
11 | public class FilterOperationDemo {
12 | public static void main(String[] args) {
13 | StepVerifier.create(Flux.range(1, 6)
14 | // 过滤奇数
15 | .filter(i -> i % 2 == 1)
16 | // 过滤后的元素进行立方操作
17 | .map(i -> i * i * i))
18 | // 期望的结果
19 | .expectNext(1, 27, 125)
20 | // 异常情况模拟
21 | //.expectNext(1, 127, 125)
22 | .verifyComplete();
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/chapter6/src/main/java/com/test/reactor/FlatMapOperationDemo.java:
--------------------------------------------------------------------------------
1 | package com.test.reactor;
2 |
3 | import reactor.core.publisher.Flux;
4 | import reactor.test.StepVerifier;
5 |
6 | import java.time.Duration;
7 |
8 | /**
9 | * @Author zhouguanya
10 | * @Date 2018/10/31
11 | * @Description flatMap操作符测试
12 | */
13 | public class FlatMapOperationDemo {
14 | public static void main(String[] args) {
15 | StepVerifier.create(
16 | Flux.just("flux", "mono")
17 | // 将每个字符串拆分为包含一个字符串的字节流
18 | .flatMap(s -> Flux.fromArray(s.split("\\s*"))
19 | //对每个元素延迟100ms
20 | .delayElements(Duration.ofMillis(1000)))
21 | // 对每个元素进行打印 doOnNext不会消费数据流
22 | .doOnNext(System.out::print))
23 | //验证是否发出了8个元素
24 | .expectNextCount(8)
25 | .verifyComplete();
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/chapter6/src/main/java/com/test/reactor/MapOperatorDemo.java:
--------------------------------------------------------------------------------
1 | package com.test.reactor;
2 |
3 | import reactor.core.publisher.Flux;
4 | import reactor.test.StepVerifier;
5 |
6 | /**
7 | * @Author zhouguanya
8 | * @Date 2018/10/30
9 | * @Description map操作符测试
10 | */
11 | public class MapOperatorDemo {
12 | public static void main(String[] args) {
13 | // 生成从1开始的,步长为1的6个整型数据
14 | StepVerifier.create(Flux.range(1, 6)
15 | // 将元素进行立方操作
16 | .map(i -> i * i * i))
17 | // 期望值
18 | .expectNext(1, 8, 27, 64, 125, 216)
19 | // 异常情况模拟
20 | //.expectNext(10, 8, 27, 64, 125, 216)
21 | // 完成信号
22 | .expectComplete()
23 | .verify();
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/chapter6/src/main/java/com/test/reactor/StepVerifierDemo.java:
--------------------------------------------------------------------------------
1 | package com.test.reactor;
2 |
3 |
4 | import reactor.core.publisher.Flux;
5 | import reactor.core.publisher.Mono;
6 | import reactor.test.StepVerifier;
7 |
8 | /**
9 | * @Author zhouguanya
10 | * @Date 2018/10/25
11 | * @Description StepVerifier测试案例
12 | */
13 | public class StepVerifierDemo {
14 | public static void main(String[] args) {
15 | Flux flux = Flux.just(1, 2, 3, 4, 5, 6);
16 | // 使用StepVerifier测试Flux,应该正常
17 | StepVerifier.create(flux)
18 | //测试下一个期望的数据元素
19 | .expectNext(1, 2, 3, 4, 5, 6)
20 | //测试下一个元素是否为完成信号
21 | .expectComplete()
22 | .verify();
23 |
24 | Mono mono = Mono.error(new Exception("some error"));
25 | // 使用StepVerifier测试Mono,应该会出现异常
26 | StepVerifier.create(mono)
27 | //测试下一个元素是否为完成信号
28 | .expectComplete()
29 | .verify();
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/chapter6/src/main/java/com/test/reactor/ZipOperationDemo.java:
--------------------------------------------------------------------------------
1 | package com.test.reactor;
2 |
3 | import reactor.core.publisher.Flux;
4 | import reactor.test.StepVerifier;
5 |
6 | import java.time.Duration;
7 |
8 | /**
9 | * @Author zhouguanya
10 | * @Date 2018/10/31
11 | * @Description zip操作符测试
12 | */
13 | public class ZipOperationDemo {
14 | public static void main(String[] args) {
15 | String desc = "I am Reactor";
16 | StepVerifier.create(
17 | // 将字符串拆分为一个一个的单词并以每500ms/个的速度发出
18 | Flux.zip(Flux.fromArray(desc.split("\\s+"))
19 | , Flux.interval(Duration.ofMillis(500)))
20 | // 打印
21 | .doOnNext(System.out::print))
22 | // 验证发出3个元素
23 | .expectNextCount(3)
24 | .verifyComplete();
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/chapter6/src/main/java/com/test/traditionalprogram/Test.java:
--------------------------------------------------------------------------------
1 | package com.test.traditionalprogram;
2 |
3 | /**
4 | * @Author zhouguanya
5 | * @Date 2018/10/10
6 | * @Description 传统编程模型 暂不考虑重排序
7 | */
8 | public class Test {
9 | public static void main(String[] args) {
10 | int a = 1;
11 | int b = 2;
12 | //c d有依赖关系
13 | int c = a + b;
14 | //如果c没有执行完,d就不能执行
15 | //对应企业开发场景中,如果c是一个远程调用,d是对远程调用结果进行分析
16 | //那么d只能等待c的结果,造成d后的程序都必须同步等待
17 | int d = c / 3;
18 | System.out.println(c);
19 | System.out.println(d);
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/chapter6/src/main/java/com/test/webflux/HelloController.java:
--------------------------------------------------------------------------------
1 | package com.test.webflux;
2 |
3 | import org.springframework.web.bind.annotation.GetMapping;
4 | import org.springframework.web.bind.annotation.RestController;
5 | import reactor.core.publisher.Mono;
6 |
7 | /**
8 | * @Author zhouguanya
9 | * @Date 2018/11/2
10 | * @Description webflux hello world
11 | */
12 | @RestController
13 | public class HelloController {
14 |
15 | @GetMapping("/helloflux")
16 | public Mono helloFlux() {
17 | return Mono.just("welcome to webflux world ~");
18 | }
19 |
20 | }
--------------------------------------------------------------------------------
/chapter7/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | project.demo
7 | com.test.spring5
8 | 1.0-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | chapter7
13 | ${spring5.chapter7.version}
14 | jar
15 |
16 |
17 | org.springframework.boot
18 | spring-boot-starter-webflux
19 | 2.1.0.RELEASE
20 |
21 |
22 |
--------------------------------------------------------------------------------
/chapter7/src/main/java/com/test/resttemplate/DateController.java:
--------------------------------------------------------------------------------
1 | package com.test.resttemplate;
2 |
3 | import org.springframework.beans.factory.annotation.Autowired;
4 | import org.springframework.web.bind.annotation.RequestMapping;
5 | import org.springframework.web.bind.annotation.RestController;
6 |
7 | /**
8 | * @Author zhouguanya
9 | * @Date 2018/11/7
10 | * @Description 控制器
11 | */
12 | @RestController("dateController")
13 | public class DateController {
14 | @Autowired
15 | private DateService dateService;
16 |
17 | @RequestMapping("/date/currentDate")
18 | public String getCurrentDate() {
19 | return dateService.queryCurrentDate();
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/chapter7/src/main/java/com/test/resttemplate/DateService.java:
--------------------------------------------------------------------------------
1 | package com.test.resttemplate;
2 |
3 | /**
4 | * @Author zhouguanya
5 | * @Date 2018/11/7
6 | * @Description 时间接口
7 | */
8 | public interface DateService {
9 | /**
10 | * 当前日期
11 | */
12 | String queryCurrentDate();
13 | }
14 |
--------------------------------------------------------------------------------
/chapter7/src/main/java/com/test/resttemplate/DateServiceImpl.java:
--------------------------------------------------------------------------------
1 | package com.test.resttemplate;
2 |
3 | import org.springframework.stereotype.Service;
4 |
5 | import java.text.SimpleDateFormat;
6 | import java.util.Date;
7 |
8 | /**
9 | * @Author zhouguanya
10 | * @Date 2018/11/7
11 | * @Description DateService接口实现
12 | */
13 | @Service
14 | public class DateServiceImpl implements DateService {
15 | /**
16 | * 当前日期
17 | */
18 | @Override
19 | public String queryCurrentDate() {
20 | SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
21 | return "Today is " + format.format(new Date());
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/chapter7/src/main/java/com/test/webclient/DateWebFluxController.java:
--------------------------------------------------------------------------------
1 | package com.test.webclient;
2 |
3 | import org.springframework.web.bind.annotation.*;
4 | import reactor.core.publisher.Mono;
5 |
6 | import javax.annotation.Resource;
7 | /**
8 | * @Author zhouguanya
9 | * @Date 2018/11/7
10 | * @Description 控制器
11 | */
12 | @RestController("dateWebFluxController")
13 | public class DateWebFluxController {
14 | @Resource
15 | private DateWebFluxService dateWebFluxService;
16 |
17 | @RequestMapping("/date/webflux/currentDate")
18 | public Mono getCurrentDate() {
19 | return dateWebFluxService.queryCurrentDate();
20 | }
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/chapter7/src/main/java/com/test/webclient/DateWebFluxService.java:
--------------------------------------------------------------------------------
1 | package com.test.webclient;
2 |
3 | import reactor.core.publisher.Mono;
4 |
5 | /**
6 | * @Author zhouguanya
7 | * @Date 2018/11/7
8 | * @Description 时间接口
9 | */
10 | public interface DateWebFluxService {
11 | /**
12 | * 当前日期
13 | */
14 | Mono queryCurrentDate();
15 | }
16 |
--------------------------------------------------------------------------------
/chapter7/src/main/java/com/test/webclient/DateWebFluxServiceImpl.java:
--------------------------------------------------------------------------------
1 | package com.test.webclient;
2 |
3 | import org.springframework.stereotype.Service;
4 | import reactor.core.publisher.Mono;
5 |
6 | import java.text.SimpleDateFormat;
7 | import java.util.Date;
8 |
9 | /**
10 | * @Author zhouguanya
11 | * @Date 2018/11/7
12 | * @Description DateService接口实现
13 | */
14 | @Service
15 | public class DateWebFluxServiceImpl implements DateWebFluxService {
16 | /**
17 | * 当前日期
18 | */
19 | @Override
20 | public Mono queryCurrentDate() {
21 | SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
22 | return Mono.just("Today is " + format.format(new Date()));
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/chapter8/src/main/kotlin/com/test/spring5/Hello.kt:
--------------------------------------------------------------------------------
1 | package com.test.spring5
2 |
3 | fun main(args: Array) {
4 | println("Hello, World")
5 | }
6 |
7 |
--------------------------------------------------------------------------------
/chapter8/src/main/kotlin/com/test/spring5/kotlindemo/clazz/KotlinAndJava.kt:
--------------------------------------------------------------------------------
1 | package com.test.spring5.kotlindemo.clazz
2 | /**
3 | * @Author zhouguanya
4 | * @Date 2018/12/8
5 | * @Description
6 | */
7 | class KotlinAndJava {
8 | /**
9 | * Kotlin与Java互操作
10 | */
11 | fun kotlinJavaInteract(source: java.util.ArrayList) {
12 | val list = ArrayList()
13 | // 使用Java中for遍历集合
14 | for (item in source) {
15 | list.add(item)
16 | }
17 | System.out.println(list)
18 | System.out.println("------分隔符------")
19 | // kotlin操作符遍历集合
20 | for (i in 0 until source.size) {
21 | System.out.println(source[i])
22 | }
23 | }
24 | }
25 |
26 |
27 | fun main(args: Array) {
28 | val city = KotlinAndJava()
29 | // 使用Java中的ArrayList类
30 | val list = java.util.ArrayList()
31 | list.add(1)
32 | list.add(2)
33 | list.add(3)
34 | list.add(4)
35 | list.add(5)
36 | city.kotlinJavaInteract(list)
37 | }
--------------------------------------------------------------------------------
/chapter8/src/main/kotlin/com/test/spring5/kotlindemo/web/SpringBootWithKotlinApplication.kt:
--------------------------------------------------------------------------------
1 | package com.test.spring5.kotlindemo.web
2 |
3 | import org.springframework.boot.SpringApplication
4 | import org.springframework.boot.autoconfigure.SpringBootApplication
5 |
6 | /**
7 | * @Author zhouguanya
8 | * @Date 2018/12/8
9 | * @Description 启动类
10 | */
11 | @SpringBootApplication
12 | open class SpringBootWithKotlinApplication fun main(args: Array) {
13 |
14 | SpringApplication.run(SpringBootWithKotlinApplication::class.java, *args)
15 |
16 | }
--------------------------------------------------------------------------------
/chapter8/src/main/kotlin/com/test/spring5/kotlindemo/web/Staff.kt:
--------------------------------------------------------------------------------
1 | package com.test.spring5.kotlindemo.web
2 |
3 | /**
4 | * @Author zhouguanya
5 | * @Date 2018/12/8
6 | * @Description 员工类
7 | */
8 | class Staff(
9 | var id: Int = -1,
10 | var name: String = ""
11 | ) {
12 | override fun toString(): String {
13 | return "Staff(id=$id, name='$name')"
14 | }
15 | }
--------------------------------------------------------------------------------
/chapter8/src/main/kotlin/com/test/spring5/kotlindemo/web/StaffController.kt:
--------------------------------------------------------------------------------
1 | package com.test.spring5.kotlindemo.web
2 |
3 | import org.springframework.web.bind.annotation.GetMapping
4 | import org.springframework.web.bind.annotation.RestController
5 |
6 | /**
7 | * @Author zhouguanya
8 | * @Date 2018/12/8
9 | * @Description 员工控制器
10 | */
11 | @RestController
12 | class StaffController(private val manService:StaffService) {
13 |
14 | @GetMapping("/staff/find")
15 | fun home(): String {
16 | val staff = manService.findByName("michael")
17 | return "staff id = "+ staff.id + ", staff name = " + staff.name
18 | }
19 |
20 | }
--------------------------------------------------------------------------------
/chapter8/src/main/kotlin/com/test/spring5/kotlindemo/web/StaffService.kt:
--------------------------------------------------------------------------------
1 | package com.test.spring5.kotlindemo.web
2 |
3 | /**
4 | * @Author zhouguanya
5 | * @Date 2018/12/8
6 | * @Description 员工接口
7 | */
8 | interface StaffService {
9 | fun findByName(name: String): Staff
10 | }
--------------------------------------------------------------------------------
/chapter8/src/main/kotlin/com/test/spring5/kotlindemo/web/StaffServiceImpl.kt:
--------------------------------------------------------------------------------
1 | package com.test.spring5.kotlindemo.web
2 |
3 | import org.springframework.stereotype.Service
4 |
5 | /**
6 | * @Author zhouguanya
7 | * @Date 2018/12/8
8 | * @Description 员工接口实现类
9 | */
10 | @Service
11 | class StaffServiceImpl : StaffService {
12 | override fun findByName(name: String): Staff {
13 | return Staff(100, name)
14 | }
15 | }
--------------------------------------------------------------------------------
/chapter8/src/test/kotlin/com/test/spring5/HelloTest.kt:
--------------------------------------------------------------------------------
1 | package com.test.spring5
2 |
3 | import org.junit.Test
4 | import kotlin.test.assertEquals
5 |
6 | class HelloTest {
7 |
8 | }
9 |
--------------------------------------------------------------------------------
/chapter9/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | project.demo
7 | com.test.spring5
8 | 1.0-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | chapter9
13 |
14 |
15 |
16 | org.junit.jupiter
17 | junit-jupiter
18 | 5.4.0
19 | test
20 |
21 |
22 |
--------------------------------------------------------------------------------
/chapter9/src/main/java/com/test/demo/Calculator.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015-2018 the original author or authors.
3 | *
4 | * All rights reserved. This program and the accompanying materials are
5 | * made available under the terms of the Eclipse Public License v2.0 which
6 | * accompanies this distribution and is available at
7 | *
8 | * http://www.eclipse.org/legal/epl-v20.html
9 | */
10 |
11 | package com.test.demo;
12 |
13 | /**
14 | * @Author: zhouguanya
15 | * @Date: 2019/02/09
16 | * @Description: 计算类
17 | */
18 | public class Calculator {
19 |
20 | public int add(int a, int b) {
21 | return a + b;
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/spring5all/src/main/java/com/test/Test.java:
--------------------------------------------------------------------------------
1 | package com.test;
2 |
3 | /**
4 | * @Author zhouguanya
5 | * @Date 2018/10/22
6 | * @Description
7 | */
8 | public class Test {
9 | }
10 |
--------------------------------------------------------------------------------
/spring5all/src/main/resources/applicationContext.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/spring5all/src/main/webapp/index.jsp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
Hello World!
4 |
5 |
6 |
--------------------------------------------------------------------------------
/spring5all/src/main/webapp/pages/hello.jsp:
--------------------------------------------------------------------------------
1 |
2 |
3 | Hello World Spring 5!
4 |
5 |
6 |
--------------------------------------------------------------------------------
/spring5all/src/main/webapp/pages/hellospringmvc.jsp:
--------------------------------------------------------------------------------
1 | <%@ page contentType="text/html; charset=UTF-8" %>
2 |
3 |
4 | Hello World
5 |
6 |
7 | Hello, ${message}
8 |
9 |
--------------------------------------------------------------------------------
/spring5all/src/main/webapp/template/helloFreeMarker.ftl:
--------------------------------------------------------------------------------
1 |
2 |
3 | Hello,${msg}
4 |
5 |
--------------------------------------------------------------------------------