.:: CODE SNIPPET ::.

"Your time is limited, so don't waste it living someone else's life"

Category Archives: Java

Configuration ngoài cho ứng dụng với Spring Boot


1. Sử dụng properties file SpringApplication sẽ load các property từ tất cả các file application.properties trong các vị trí sau, và thêm chúng vào application environment: + Folder /config bên trong folder hiện tại. + Folder hiện tại. + Class path /config + Root class path Mặc định, SpringApplication sẽ load theo thứ tự từ dưới lên trên. Và các property nào load sau sẽ override property load trước. Vì lý do này, bạn cứ đặt property vào class path bình thường, khi muốn config ngoài file jar, thì sẽ đặt file application.properties vào folder /config là được. Như vậy cần phải build dự án với maven sao cho có kết quả trong folder target như sau:

+-target
    +---myapp.jar
    +---config
            +--application.properties

Read more of this post

Advertisements

Sử dụng configuration class trong Spring Boot


1. Sử dụng class Configuration
Spring Boot ưu ái việc sử dụng các class Configuration. Mặc dù bạn có thể hoàn toàn sử dụng việc gọi hàm SpringApplication.run() ngay bên trong file XML, nhưng nhìn chung Spring khuyên điểm xuất phát của ứng dụng là một class với annotation @Configuration. Thông thường thì class với hàm main() thì nên là cũng là lớp chứa @Configuration.

import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.Configuration;

@Configuration
public class Application
{
    public static void main(final String[] args)
    {
        SpringApplication.run(Application.class, args);
    }
}

Read more of this post

Extract content and attachments from an Message use Javax mail (with embedded message EML, MSG)


The main part in a javax message is the content. The content can be any Java object representing the mail content, like a plain text (String) or raw image data or even a multiple part object. A multiple part content a message’s content consists of more than a single item. A multiple part object is a container which holds one ore more BodyPart objects. These BodyParts, like a message, have a content and a content type (in fact, both message and BodyPart implement the same interface Part which carries these properties).

Beside plain content, A BodyPart can contain another Multipart or even another Message, a so called nested message (e.g. a forwarded message, called embedded message) with content type ‘message/rfc822’.

As you can see, the structure of a message can be rather heterogeneous, a tree with nodes of different types. The following picture illustrates the tree structure for a sample message.
Read more of this post

Install plugins for eclipse Luna via dropins directory


The simple way you can install plugins for eclipse Luna is create an external dropins directory. In this dropins directory you can easily download plugins and extract it to each folder. You don’t need to do anything else. The dropins folder after contain 3 plugins: Windowbuilder, Checkstyle and Jautodoc have the structure as below:

And the only other thing you have to do is add a property contain the absolute path to your dropins directory into eclipse.ini file.

-Dorg.eclipse.equinox.p2.reconciler.dropins.directory=d:\work\common\lib\eclipse_extension\extensions\dropins

Searching algorithm DFS


Depth first search (DFS) is a searching algorithm used in searching maze.
Prerequisites:
A maze is also a graph with: a vertex equals a intersection, and edge is passage. (So, we should have some basic knowledge about graph, this will is be stored in some next posts)
The goal of using this algorithm in maze is explore all the connected intersections with a start point in the maze.
Algorithm for DFS:
+ Mark the visited intersection by a nut on the string.
+ Enter any passage related with that intersection that have never visit before and unroll the ball of string to mark that passage.
+ Retrace steps when no unvisited options.
Read more of this post

Basically usage of properties file in Java project


Properties file is just a file whose extension is properties in a Java project. It simply contains the pair of key-value string separated by a equal character. Properties file is used for obtaining configuration information, interface messages,…for usual. In generally, it is used for some circumstances in which we need to change the information easily. What functions does Java suppose us to do with a properties files? They comprise loading all information stored in properties file and listing them, retrieving any key-value pair, updating the value of a pair. These actions are all via a Properties object. The following snippets depict the functions:
Getting a value from a key:
Read more of this post

How to customize GUI base on user role with spring security


To restrict user from seeing one or more parts on any page you can use a function of spring security that check user login ‘s role. Snippet below is an example of the granted authority checking:

<sec:ifAnyGranted roles="ROLE_ADMIN">
    <li><g:link class="create" action="create">
            <g:message code="default.new.label" args="[entityName]" />
        </g:link></li>
</sec:ifAnyGranted>

The code restrict user from access a button with user role to only ADMIN. If we pre-define the role hierarchy of roles in system, we just only specify one role, otherwise you have to declare all the roles that can see the inside elements.

%d bloggers like this: